vdr 2.8.2
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 5.23 2026/05/05 14:41:31 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:
20static bool DebugPatPmt = false;
21static 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
32ePesHeader 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 ...
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) {
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
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
102void 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
131void 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
147int 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
160int64_t TsGetPts(const uchar *p, int l, int Pid)
161{
162 // Find the first packet with a PTS and use it:
163 while (l > 0) {
164 if (Pid < 0 || TsPid(p) == Pid) {
165 const uchar *d = p;
166 if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasPts(d))
167 return PesGetPts(d);
168 }
169 p += TS_SIZE;
170 l -= TS_SIZE;
171 }
172 return -1;
173}
174
175int64_t TsGetDts(const uchar *p, int l)
176{
177 // Find the first packet with a DTS and use it:
178 while (l > 0) {
179 const uchar *d = p;
180 if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasDts(d))
181 return PesGetDts(d);
182 p += TS_SIZE;
183 l -= TS_SIZE;
184 }
185 return -1;
186}
187
188void TsSetPts(uchar *p, int l, int64_t Pts)
189{
190 // Find the first packet with a PTS and use it:
191 while (l > 0) {
192 const uchar *d = p;
193 if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasPts(d)) {
194 PesSetPts(const_cast<uchar *>(d), Pts);
195 return;
196 }
197 p += TS_SIZE;
198 l -= TS_SIZE;
199 }
200}
201
202void TsSetDts(uchar *p, int l, int64_t Dts)
203{
204 // Find the first packet with a DTS and use it:
205 while (l > 0) {
206 const uchar *d = p;
207 if (TsPayloadStart(d) && TsGetPayload(&d) && PesHasDts(d)) {
208 PesSetDts(const_cast<uchar *>(d), Dts);
209 return;
210 }
211 p += TS_SIZE;
212 l -= TS_SIZE;
213 }
214}
215
216// --- Some PES handling tools -----------------------------------------------
217
218void PesSetPts(uchar *p, int64_t Pts)
219{
220 p[ 9] = ((Pts >> 29) & 0x0E) | (p[9] & 0xF1);
221 p[10] = Pts >> 22;
222 p[11] = ((Pts >> 14) & 0xFE) | 0x01;
223 p[12] = Pts >> 7;
224 p[13] = ((Pts << 1) & 0xFE) | 0x01;
225}
226
227void PesSetDts(uchar *p, int64_t Dts)
228{
229 p[14] = ((Dts >> 29) & 0x0E) | (p[14] & 0xF1);
230 p[15] = Dts >> 22;
231 p[16] = ((Dts >> 14) & 0xFE) | 0x01;
232 p[17] = Dts >> 7;
233 p[18] = ((Dts << 1) & 0xFE) | 0x01;
234}
235
236int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
237{
238 int64_t d = Pts2 - Pts1;
239 if (d > MAX33BIT / 2)
240 return d - (MAX33BIT + 1);
241 if (d < -MAX33BIT / 2)
242 return d + (MAX33BIT + 1);
243 return d;
244}
245
246// --- cTsPayload ------------------------------------------------------------
247
249{
250 data = NULL;
251 length = 0;
252 pid = -1;
253 Reset();
254}
255
256cTsPayload::cTsPayload(uchar *Data, int Length, int Pid)
257{
258 Setup(Data, Length, Pid);
259}
260
262{
263 length = index; // triggers EOF
264 return 0x00;
265}
266
268{
269 index = 0;
270 numPacketsPid = 0;
271 numPacketsOther = 0;
272}
273
274void cTsPayload::Setup(uchar *Data, int Length, int Pid)
275{
276 data = Data;
277 length = Length;
278 pid = Pid >= 0 ? Pid : TsPid(Data);
279 Reset();
280}
281
283{
284 if (!Eof()) {
285 if (index % TS_SIZE == 0) { // encountered the next TS header
286 for (;; index += TS_SIZE) {
287 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
288 uchar *p = data + index;
289 if (TsPid(p) == pid) { // only handle TS packets for the initial PID
291 return SetEof();
292 if (TsError(p))
293 return SetEof(); // don't parse TS packets with errors
294 if (TsHasPayload(p)) {
295 if (index > 0 && TsPayloadStart(p)) // checking index to not skip the very first TS packet
296 return SetEof();
298 break;
299 }
300 }
301 else if (TsPid(p) == PATPID)
302 return SetEof(); // caller must see PAT packets in case of index regeneration
303 else
305 }
306 else
307 return SetEof();
308 }
309 }
310 return data[index++];
311 }
312 return 0x00;
313}
314
316{
317 while (Bytes-- > 0)
318 GetByte();
319 return !Eof();
320}
321
326
328{
329 return index - 1;
330}
331
332void cTsPayload::SetByte(uchar Byte, int Index)
333{
334 if (Index >= 0 && Index < length)
335 data[Index] = Byte;
336}
337
338bool cTsPayload::Find(uint32_t Code)
339{
340 int OldIndex = index;
341 int OldNumPacketsPid = numPacketsPid;
342 int OldNumPacketsOther = numPacketsOther;
343 uint32_t Scanner = EMPTY_SCANNER;
344 while (!Eof()) {
345 Scanner = (Scanner << 8) | GetByte();
346 if (Scanner == Code)
347 return true;
348 }
349 index = OldIndex;
350 numPacketsPid = OldNumPacketsPid;
351 numPacketsOther = OldNumPacketsOther;
352 return false;
353}
354
356{
358 dsyslog("WARNING: required (%d+%d) TS packets to determine frame type", numPacketsOther, numPacketsPid);
360 dsyslog("WARNING: required %d video TS packets to determine frame type", numPacketsPid);
361}
362
363// --- cPatPmtGenerator ------------------------------------------------------
364
366{
367 numPmtPackets = 0;
370 pmtPid = 0;
371 esInfoLength = NULL;
372 SetChannel(Channel);
373}
374
375void cPatPmtGenerator::IncCounter(int &Counter, uchar *TsPacket)
376{
377 TsPacket[3] = (TsPacket[3] & 0xF0) | Counter;
378 if (++Counter > 0x0F)
379 Counter = 0x00;
380}
381
383{
384 if (++Version > 0x1F)
385 Version = 0x00;
386}
387
389{
390 if (esInfoLength) {
391 Length += ((*esInfoLength & 0x0F) << 8) | *(esInfoLength + 1);
392 *esInfoLength = 0xF0 | (Length >> 8);
393 *(esInfoLength + 1) = Length;
394 }
395}
396
397int cPatPmtGenerator::MakeStream(uchar *Target, uchar Type, int Pid)
398{
399 int i = 0;
400 Target[i++] = Type; // stream type
401 Target[i++] = 0xE0 | (Pid >> 8); // dummy (3), pid hi (5)
402 Target[i++] = Pid; // pid lo
403 esInfoLength = &Target[i];
404 Target[i++] = 0xF0; // dummy (4), ES info length hi
405 Target[i++] = 0x00; // ES info length lo
406 return i;
407}
408
410{
411 int i = 0;
412 Target[i++] = Type;
413 Target[i++] = 0x01; // length
414 Target[i++] = 0x00;
416 return i;
417}
418
419int cPatPmtGenerator::MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
420{
421 int i = 0;
422 Target[i++] = SI::SubtitlingDescriptorTag;
423 Target[i++] = 0x08; // length
424 Target[i++] = *Language++;
425 Target[i++] = *Language++;
426 Target[i++] = *Language++;
427 Target[i++] = SubtitlingType;
428 Target[i++] = CompositionPageId >> 8;
429 Target[i++] = CompositionPageId & 0xFF;
430 Target[i++] = AncillaryPageId >> 8;
431 Target[i++] = AncillaryPageId & 0xFF;
433 return i;
434}
435
436int cPatPmtGenerator::MakeLanguageDescriptor(uchar *Target, const char *Language)
437{
438 int i = 0;
440 int Length = i++;
441 Target[Length] = 0x00; // length
442 for (const char *End = Language + strlen(Language); Language < End; ) {
443 Target[i++] = *Language++;
444 Target[i++] = *Language++;
445 Target[i++] = *Language++;
446 Target[i++] = 0x00; // audio type
447 Target[Length] += 0x04; // length
448 if (*Language == '+')
449 Language++;
450 }
452 return i;
453}
454
455int cPatPmtGenerator::MakeCRC(uchar *Target, const uchar *Data, int Length)
456{
457 int crc = SI::CRC32::crc32((const char *)Data, Length, 0xFFFFFFFF);
458 int i = 0;
459 Target[i++] = crc >> 24;
460 Target[i++] = crc >> 16;
461 Target[i++] = crc >> 8;
462 Target[i++] = crc;
463 return i;
464}
465
466#define P_TSID 0x8008 // pseudo TS ID
467#define P_PMT_PID 0x0084 // pseudo PMT pid
468#define MAXPID 0x2000 // the maximum possible number of pids
469
471{
472 bool Used[MAXPID] = { false };
473#define SETPID(p) { if ((p) >= 0 && (p) < MAXPID) Used[p] = true; }
474#define SETPIDS(l) { const int *p = l; while (*p) { SETPID(*p); p++; } }
475 SETPID(Channel->Vpid());
476 SETPID(Channel->Ppid());
477 SETPID(Channel->Tpid());
478 SETPIDS(Channel->Apids());
479 SETPIDS(Channel->Dpids());
480 SETPIDS(Channel->Spids());
481 for (pmtPid = P_PMT_PID; Used[pmtPid]; pmtPid++)
482 ;
483}
484
486{
487 memset(pat, 0xFF, sizeof(pat));
488 uchar *p = pat;
489 int i = 0;
490 p[i++] = TS_SYNC_BYTE; // TS indicator
491 p[i++] = TS_PAYLOAD_START | (PATPID >> 8); // flags (3), pid hi (5)
492 p[i++] = PATPID & 0xFF; // pid lo
493 p[i++] = 0x10; // flags (4), continuity counter (4)
494 p[i++] = 0x00; // pointer field (payload unit start indicator is set)
495 int PayloadStart = i;
496 p[i++] = 0x00; // table id
497 p[i++] = 0xB0; // section syntax indicator (1), dummy (3), section length hi (4)
498 int SectionLength = i;
499 p[i++] = 0x00; // section length lo (filled in later)
500 p[i++] = P_TSID >> 8; // TS id hi
501 p[i++] = P_TSID & 0xFF; // TS id lo
502 p[i++] = 0xC1 | (patVersion << 1); // dummy (2), version number (5), current/next indicator (1)
503 p[i++] = 0x00; // section number
504 p[i++] = 0x00; // last section number
505 p[i++] = pmtPid >> 8; // program number hi
506 p[i++] = pmtPid & 0xFF; // program number lo
507 p[i++] = 0xE0 | (pmtPid >> 8); // dummy (3), PMT pid hi (5)
508 p[i++] = pmtPid & 0xFF; // PMT pid lo
509 pat[SectionLength] = i - SectionLength - 1 + 4; // -1 = SectionLength storage, +4 = length of CRC
510 MakeCRC(pat + i, pat + PayloadStart, i - PayloadStart);
512}
513
515{
516 // generate the complete PMT section:
518 memset(buf, 0xFF, sizeof(buf));
519 numPmtPackets = 0;
520 if (Channel) {
521 int Vpid = Channel->Vpid();
522 int Ppid = Channel->Ppid();
523 uchar *p = buf;
524 int i = 0;
525 p[i++] = 0x02; // table id
526 int SectionLength = i;
527 p[i++] = 0xB0; // section syntax indicator (1), dummy (3), section length hi (4)
528 p[i++] = 0x00; // section length lo (filled in later)
529 p[i++] = pmtPid >> 8; // program number hi
530 p[i++] = pmtPid & 0xFF; // program number lo
531 p[i++] = 0xC1 | (pmtVersion << 1); // dummy (2), version number (5), current/next indicator (1)
532 p[i++] = 0x00; // section number
533 p[i++] = 0x00; // last section number
534 p[i++] = 0xE0 | (Ppid >> 8); // dummy (3), PCR pid hi (5)
535 p[i++] = Ppid; // PCR pid lo
536 p[i++] = 0xF0; // dummy (4), program info length hi (4)
537 p[i++] = 0x00; // program info length lo
538
539 if (Vpid)
540 i += MakeStream(buf + i, Channel->Vtype(), Vpid);
541 for (int n = 0; Channel->Apid(n); n++) {
542 i += MakeStream(buf + i, Channel->Atype(n), Channel->Apid(n));
543 const char *Alang = Channel->Alang(n);
544 i += MakeLanguageDescriptor(buf + i, Alang);
545 }
546 for (int n = 0; Channel->Dpid(n); n++) {
547 i += MakeStream(buf + i, 0x06, Channel->Dpid(n));
548 i += MakeAC3Descriptor(buf + i, Channel->Dtype(n));
549 i += MakeLanguageDescriptor(buf + i, Channel->Dlang(n));
550 }
551 for (int n = 0; Channel->Spid(n); n++) {
552 i += MakeStream(buf + i, 0x06, Channel->Spid(n));
553 i += MakeSubtitlingDescriptor(buf + i, Channel->Slang(n), Channel->SubtitlingType(n), Channel->CompositionPageId(n), Channel->AncillaryPageId(n));
554 }
555
556 int sl = i - SectionLength - 2 + 4; // -2 = SectionLength storage, +4 = length of CRC
557 buf[SectionLength] |= (sl >> 8) & 0x0F;
558 buf[SectionLength + 1] = sl;
559 MakeCRC(buf + i, buf, i);
560 // split the PMT section into several TS packets:
561 uchar *q = buf;
562 bool pusi = true;
563 while (i > 0) {
564 uchar *p = pmt[numPmtPackets++];
565 int j = 0;
566 p[j++] = TS_SYNC_BYTE; // TS indicator
567 p[j++] = (pusi ? TS_PAYLOAD_START : 0x00) | (pmtPid >> 8); // flags (3), pid hi (5)
568 p[j++] = pmtPid & 0xFF; // pid lo
569 p[j++] = 0x10; // flags (4), continuity counter (4)
570 if (pusi) {
571 p[j++] = 0x00; // pointer field (payload unit start indicator is set)
572 pusi = false;
573 }
574 int l = TS_SIZE - j;
575 memcpy(p + j, q, l);
576 q += l;
577 i -= l;
578 }
580 }
581}
582
583void cPatPmtGenerator::SetVersions(int PatVersion, int PmtVersion)
584{
585 patVersion = PatVersion & 0x1F;
586 pmtVersion = PmtVersion & 0x1F;
587}
588
590{
591 if (Channel) {
592 GeneratePmtPid(Channel);
593 GeneratePat();
594 GeneratePmt(Channel);
595 }
596}
597
599{
601 return pat;
602}
603
605{
606 if (Index < numPmtPackets) {
607 IncCounter(pmtCounter, pmt[Index]);
608 return pmt[Index++];
609 }
610 return NULL;
611}
612
613// --- cPatPmtParser ---------------------------------------------------------
614
615cPatPmtParser::cPatPmtParser(bool UpdatePrimaryDevice)
616{
617 updatePrimaryDevice = UpdatePrimaryDevice;
618 Reset();
619}
620
622{
623 completed = false;
624 pmtSize = 0;
625 patVersion = pmtVersion = -1;
626 pmtPids[0] = 0;
627 vpid = vtype = 0;
628 ppid = 0;
629}
630
631void cPatPmtParser::ParsePat(const uchar *Data, int Length)
632{
633 // Unpack the TS packet:
634 int PayloadOffset = TsPayloadOffset(Data);
635 Data += PayloadOffset;
636 Length -= PayloadOffset;
637 // The PAT is always assumed to fit into a single TS packet
638 if ((Length -= Data[0] + 1) <= 0)
639 return;
640 Data += Data[0] + 1; // process pointer_field
641 SI::PAT Pat(Data, false);
642 if (Pat.CheckCRCAndParse()) {
643 dbgpatpmt("PAT: TSid = %d, c/n = %d, v = %d, s = %d, ls = %d\n", Pat.getTransportStreamId(), Pat.getCurrentNextIndicator(), Pat.getVersionNumber(), Pat.getSectionNumber(), Pat.getLastSectionNumber());
644 if (patVersion == Pat.getVersionNumber())
645 return;
646 int NumPmtPids = 0;
648 for (SI::Loop::Iterator it; Pat.associationLoop.getNext(assoc, it); ) {
649 dbgpatpmt(" isNITPid = %d\n", assoc.isNITPid());
650 if (!assoc.isNITPid()) {
651 if (NumPmtPids <= MAX_PMT_PIDS)
652 pmtPids[NumPmtPids++] = assoc.getPid();
653 dbgpatpmt(" service id = %d, pid = %d\n", assoc.getServiceId(), assoc.getPid());
654 }
655 }
656 pmtPids[NumPmtPids] = 0;
658 }
659 else
660 esyslog("ERROR: can't parse PAT");
661}
662
663void cPatPmtParser::ParsePmt(const uchar *Data, int Length)
664{
665 // Unpack the TS packet:
666 bool PayloadStart = TsPayloadStart(Data);
667 int PayloadOffset = TsPayloadOffset(Data);
668 Data += PayloadOffset;
669 Length -= PayloadOffset;
670 // The PMT may extend over several TS packets, so we need to assemble them
671 if (PayloadStart) {
672 pmtSize = 0;
673 if ((Length -= Data[0] + 1) <= 0)
674 return;
675 Data += Data[0] + 1; // this is the first packet
676 if (SectionLength(Data, Length) > Length) {
677 if (Length <= int(sizeof(pmt))) {
678 memcpy(pmt, Data, Length);
679 pmtSize = Length;
680 }
681 else
682 esyslog("ERROR: PMT packet length too big (%d byte)!", Length);
683 return;
684 }
685 // the packet contains the entire PMT section, so we run into the actual parsing
686 }
687 else if (pmtSize > 0) {
688 // this is a following packet, so we add it to the pmt storage
689 if (Length <= int(sizeof(pmt)) - pmtSize) {
690 memcpy(pmt + pmtSize, Data, Length);
691 pmtSize += Length;
692 }
693 else {
694 esyslog("ERROR: PMT section length too big (%d byte)!", pmtSize + Length);
695 pmtSize = 0;
696 }
698 return; // more packets to come
699 // the PMT section is now complete, so we run into the actual parsing
700 Data = pmt;
701 }
702 else
703 return; // fragment of broken packet - ignore
704 SI::PMT Pmt(Data, false);
705 if (Pmt.CheckCRCAndParse()) {
706 dbgpatpmt("PMT: sid = %d, c/n = %d, v = %d, s = %d, ls = %d\n", Pmt.getServiceId(), Pmt.getCurrentNextIndicator(), Pmt.getVersionNumber(), Pmt.getSectionNumber(), Pmt.getLastSectionNumber());
707 dbgpatpmt(" pcr = %d\n", Pmt.getPCRPid());
708 if (pmtVersion == Pmt.getVersionNumber())
709 return;
712 int NumApids = 0;
713 int NumDpids = 0;
714 int NumSpids = 0;
715 vpid = vtype = 0;
716 ppid = 0;
717 apids[0] = 0;
718 dpids[0] = 0;
719 spids[0] = 0;
720 atypes[0] = 0;
721 dtypes[0] = 0;
722 SI::PMT::Stream stream;
723 for (SI::Loop::Iterator it; Pmt.streamLoop.getNext(stream, it); ) {
724 dbgpatpmt(" stream type = %02X, pid = %d", stream.getStreamType(), stream.getPid());
725 switch (stream.getStreamType()) {
726 case 0x01: // STREAMTYPE_11172_VIDEO
727 case 0x02: // STREAMTYPE_13818_VIDEO
728 case 0x1B: // H.264
729 case 0x24: // H.265
730 vpid = stream.getPid();
731 vtype = stream.getStreamType();
732 ppid = Pmt.getPCRPid();
733 break;
734 case 0x03: // STREAMTYPE_11172_AUDIO
735 case 0x04: // STREAMTYPE_13818_AUDIO
736 case 0x0F: // ISO/IEC 13818-7 Audio with ADTS transport syntax
737 case 0x11: // ISO/IEC 14496-3 Audio with LATM transport syntax
738 {
739 if (NumApids < MAXAPIDS) {
740 apids[NumApids] = stream.getPid();
741 atypes[NumApids] = stream.getStreamType();
742 *alangs[NumApids] = 0;
744 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
745 switch (d->getDescriptorTag()) {
749 char *s = alangs[NumApids];
750 int n = 0;
751 for (SI::Loop::Iterator it; ld->languageLoop.getNext(l, it); ) {
752 if (*ld->languageCode != '-') { // some use "---" to indicate "none"
753 dbgpatpmt(" '%s'", l.languageCode);
754 if (n > 0)
755 *s++ = '+';
757 s += strlen(s);
758 if (n++ > 1)
759 break;
760 }
761 }
762 }
763 break;
764 default: ;
765 }
766 delete d;
767 }
769 cDevice::PrimaryDevice()->SetAvailableTrack(ttAudio, NumApids, apids[NumApids], alangs[NumApids]);
770 NumApids++;
771 apids[NumApids] = 0;
772 }
773 }
774 break;
775 case 0x06: // STREAMTYPE_13818_PES_PRIVATE
776 {
777 int dpid = 0;
778 int dtype = 0;
779 char lang[MAXLANGCODE1] = "";
781 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
782 switch (d->getDescriptorTag()) {
785 dbgpatpmt(" AC3");
786 dpid = stream.getPid();
787 dtype = d->getDescriptorTag();
788 break;
790 dbgpatpmt(" subtitling");
791 if (NumSpids < MAXSPIDS) {
792 spids[NumSpids] = stream.getPid();
793 *slangs[NumSpids] = 0;
794 subtitlingTypes[NumSpids] = 0;
795 compositionPageIds[NumSpids] = 0;
796 ancillaryPageIds[NumSpids] = 0;
799 char *s = slangs[NumSpids];
800 int n = 0;
801 for (SI::Loop::Iterator it; sd->subtitlingLoop.getNext(sub, it); ) {
802 if (sub.languageCode[0]) {
803 dbgpatpmt(" '%s'", sub.languageCode);
804 subtitlingTypes[NumSpids] = sub.getSubtitlingType();
806 ancillaryPageIds[NumSpids] = sub.getAncillaryPageId();
807 if (n > 0)
808 *s++ = '+';
810 s += strlen(s);
811 if (n++ > 1)
812 break;
813 }
814 }
816 cDevice::PrimaryDevice()->SetAvailableTrack(ttSubtitle, NumSpids, spids[NumSpids], slangs[NumSpids]);
817 NumSpids++;
818 spids[NumSpids] = 0;
819 }
820 break;
823 dbgpatpmt(" '%s'", ld->languageCode);
825 }
826 break;
827 default: ;
828 }
829 delete d;
830 }
831 if (dpid) {
832 if (NumDpids < MAXDPIDS) {
833 dpids[NumDpids] = dpid;
834 dtypes[NumDpids] = dtype;
835 strn0cpy(dlangs[NumDpids], lang, sizeof(dlangs[NumDpids]));
836 if (updatePrimaryDevice && Setup.UseDolbyDigital)
837 cDevice::PrimaryDevice()->SetAvailableTrack(ttDolby, NumDpids, dpid, lang);
838 NumDpids++;
839 dpids[NumDpids] = 0;
840 }
841 }
842 }
843 break;
844 case 0x81: // STREAMTYPE_USER_PRIVATE - AC3 audio for ATSC and BD
845 case 0x82: // STREAMTYPE_USER_PRIVATE - DTS audio for BD
846 case 0x87: // eac3
847 {
848 dbgpatpmt(" %s",
849 stream.getStreamType() == 0x81 ? "AC3" :
850 stream.getStreamType() == 0x87 ? "AC3" :
851 stream.getStreamType() == 0x82 ? "DTS" : "");
852 char lang[MAXLANGCODE1] = { 0 };
854 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
855 switch (d->getDescriptorTag()) {
858 dbgpatpmt(" '%s'", ld->languageCode);
860 }
861 break;
862 default: ;
863 }
864 delete d;
865 }
866 if (NumDpids < MAXDPIDS) {
867 dpids[NumDpids] = stream.getPid();
868 dtypes[NumDpids] = SI::AC3DescriptorTag;
869 strn0cpy(dlangs[NumDpids], lang, sizeof(dlangs[NumDpids]));
870 if (updatePrimaryDevice && Setup.UseDolbyDigital)
871 cDevice::PrimaryDevice()->SetAvailableTrack(ttDolby, NumDpids, stream.getPid(), lang);
872 NumDpids++;
873 dpids[NumDpids] = 0;
874 }
875 }
876 break;
877 case 0x90: // PGS subtitles for BD
878 {
879 dbgpatpmt(" subtitling");
880 char lang[MAXLANGCODE1] = { 0 };
882 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
883 switch (d->getDescriptorTag()) {
886 dbgpatpmt(" '%s'", ld->languageCode);
888 if (NumSpids < MAXSPIDS) {
889 spids[NumSpids] = stream.getPid();
890 *slangs[NumSpids] = 0;
891 subtitlingTypes[NumSpids] = 0;
892 compositionPageIds[NumSpids] = 0;
893 ancillaryPageIds[NumSpids] = 0;
895 cDevice::PrimaryDevice()->SetAvailableTrack(ttSubtitle, NumSpids, stream.getPid(), lang);
896 NumSpids++;
897 spids[NumSpids] = 0;
898 }
899 }
900 break;
901 default: ;
902 }
903 delete d;
904 }
905 }
906 break;
907 default: ;
908 }
909 dbgpatpmt("\n");
913 }
914 }
916 completed = true;
917 }
918 else
919 esyslog("ERROR: can't parse PMT");
920 pmtSize = 0;
921}
922
923bool cPatPmtParser::ParsePatPmt(const uchar *Data, int Length)
924{
925 while (Length >= TS_SIZE) {
926 if (*Data != TS_SYNC_BYTE)
927 break; // just for safety
928 int Pid = TsPid(Data);
929 if (Pid == PATPID)
930 ParsePat(Data, TS_SIZE);
931 else if (IsPmtPid(Pid)) {
932 ParsePmt(Data, TS_SIZE);
933 if (patVersion >= 0 && pmtVersion >= 0)
934 return true;
935 }
936 Data += TS_SIZE;
937 Length -= TS_SIZE;
938 }
939 return false;
940}
941
942bool cPatPmtParser::GetVersions(int &PatVersion, int &PmtVersion) const
943{
944 PatVersion = patVersion;
945 PmtVersion = pmtVersion;
946 return patVersion >= 0 && pmtVersion >= 0;
947}
948
949// --- cEitGenerator ---------------------------------------------------------
950
952{
953 counter = 0;
954 version = 0;
955 if (Sid)
956 Generate(Sid);
957}
958
959uint16_t cEitGenerator::YMDtoMJD(int Y, int M, int D)
960{
961 int L = (M < 3) ? 1 : 0;
962 return 14956 + D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
963}
964
966{
968 *p++ = 0x04; // descriptor length
969 *p++ = '9'; // country code "902" ("All countries") -> EN 300 468 / 6.2.28; www.dvbservices.com/country_codes/index.php
970 *p++ = '0';
971 *p++ = '2';
972 *p++ = ParentalRating;
973 return p;
974}
975
977{
978 uchar *PayloadStart;
979 uchar *SectionStart;
980 uchar *DescriptorsStart;
981 memset(eit, 0xFF, sizeof(eit));
982 struct tm tm_r;
983 time_t t = time(NULL) - 3600; // let's have the event start one hour in the past
984 tm *tm = localtime_r(&t, &tm_r);
985 uint16_t MJD = YMDtoMJD(tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
986 uchar *p = eit;
987 // TS header:
988 *p++ = TS_SYNC_BYTE;
989 *p++ = TS_PAYLOAD_START;
990 *p++ = EITPID;
991 *p++ = 0x10 | (counter++ & 0x0F); // continuity counter
992 *p++ = 0x00; // pointer field (payload unit start indicator is set)
993 // payload:
994 PayloadStart = p;
995 *p++ = 0x4E; // TID present/following event on this transponder
996 *p++ = 0xF0;
997 *p++ = 0x00; // section length
998 SectionStart = p;
999 *p++ = Sid >> 8;
1000 *p++ = Sid & 0xFF;
1001 *p++ = 0xC1 | (version << 1);
1002 *p++ = 0x00; // section number
1003 *p++ = 0x00; // last section number
1004 *p++ = 0x00; // transport stream id
1005 *p++ = 0x00; // ...
1006 *p++ = 0x00; // original network id
1007 *p++ = 0x00; // ...
1008 *p++ = 0x00; // segment last section number
1009 *p++ = 0x4E; // last table id
1010 *p++ = 0x00; // event id
1011 *p++ = 0x01; // ...
1012 *p++ = MJD >> 8; // start time
1013 *p++ = MJD & 0xFF; // ...
1014 *p++ = tm->tm_hour; // ...
1015 *p++ = tm->tm_min; // ...
1016 *p++ = tm->tm_sec; // ...
1017 *p++ = 0x24; // duration (one day, should cover everything)
1018 *p++ = 0x00; // ...
1019 *p++ = 0x00; // ...
1020 *p++ = 0x90; // running status, free/CA mode
1021 *p++ = 0x00; // descriptors loop length
1022 DescriptorsStart = p;
1024 // fill in lengths:
1025 *(SectionStart - 1) = p - SectionStart + 4; // +4 = length of CRC
1026 *(DescriptorsStart - 1) = p - DescriptorsStart;
1027 // checksum
1028 int crc = SI::CRC32::crc32((char *)PayloadStart, p - PayloadStart, 0xFFFFFFFF);
1029 *p++ = crc >> 24;
1030 *p++ = crc >> 16;
1031 *p++ = crc >> 8;
1032 *p++ = crc;
1033 return eit;
1034}
1035
1036// --- cTsToPes --------------------------------------------------------------
1037
1039{
1040 data = NULL;
1041 size = 0;
1042 Reset();
1043}
1044
1046{
1047 free(data);
1048}
1049
1050void cTsToPes::PutTs(const uchar *Data, int Length)
1051{
1052 if (TsError(Data)) {
1053 Reset();
1054 return; // ignore packets with TEI set, and drop any PES data collected so far
1055 }
1056 if (TsPayloadStart(Data))
1057 Reset();
1058 else if (!size)
1059 return; // skip everything before the first payload start
1060 Length = TsGetPayload(&Data);
1061 if (length + Length > size) {
1062 int NewSize = max(KILOBYTE(2), length + Length);
1063 if (uchar *NewData = (uchar *)realloc(data, NewSize)) {
1064 data = NewData;
1065 size = NewSize;
1066 }
1067 else {
1068 esyslog("ERROR: out of memory");
1069 Reset();
1070 return;
1071 }
1072 }
1073 memcpy(data + length, Data, Length);
1074 length += Length;
1075}
1076
1077#define MAXPESLENGTH 0xFFF0
1078
1079const uchar *cTsToPes::GetPes(int &Length)
1080{
1081 if (repeatLast) {
1082 repeatLast = false;
1083 Length = lastLength;
1084 return lastData;
1085 }
1086 if (offset < length && PesLongEnough(length)) {
1087 if (!PesHasLength(data)) // this is a video PES packet with undefined length
1088 offset = 6; // trigger setting PES length for initial slice
1089 if (offset) {
1090 uchar *p = data + offset - 6;
1091 if (p != data) {
1092 p -= 3;
1093 if (p < data) {
1094 Reset();
1095 return NULL;
1096 }
1097 memmove(p, data, 4);
1098 }
1099 int l = min(length - offset, MAXPESLENGTH);
1100 offset += l;
1101 if (p != data) {
1102 l += 3;
1103 p[6] = 0x80;
1104 p[7] = 0x00;
1105 p[8] = 0x00;
1106 }
1107 p[4] = l / 256;
1108 p[5] = l & 0xFF;
1109 Length = l + 6;
1110 lastLength = Length;
1111 lastData = p;
1112 return p;
1113 }
1114 else {
1115 Length = PesLength(data);
1116 if (Length <= length) {
1117 offset = Length; // to make sure we break out in case of garbage data
1118 lastLength = Length;
1119 lastData = data;
1120 return data;
1121 }
1122 }
1123 }
1124 return NULL;
1125}
1126
1128{
1129 repeatLast = true;
1130}
1131
1133{
1134 length = offset = 0;
1135 lastData = NULL;
1136 lastLength = 0;
1137 repeatLast = false;
1138}
1139
1140// --- Some helper functions for debugging -----------------------------------
1141
1142void BlockDump(const char *Name, const u_char *Data, int Length)
1143{
1144 printf("--- %s\n", Name);
1145 for (int i = 0; i < Length; i++) {
1146 if (i && (i % 16) == 0)
1147 printf("\n");
1148 printf(" %02X", Data[i]);
1149 }
1150 printf("\n");
1151}
1152
1153void TsDump(const char *Name, const u_char *Data, int Length)
1154{
1155 printf("%s: %04X", Name, Length);
1156 int n = min(Length, 20);
1157 for (int i = 0; i < n; i++)
1158 printf(" %02X", Data[i]);
1159 if (n < Length) {
1160 printf(" ...");
1161 n = max(n, Length - 10);
1162 for (n = max(n, Length - 10); n < Length; n++)
1163 printf(" %02X", Data[n]);
1164 }
1165 printf("\n");
1166}
1167
1168void PesDump(const char *Name, const u_char *Data, int Length)
1169{
1170 TsDump(Name, Data, Length);
1171}
1172
1173// --- cFrameParser ----------------------------------------------------------
1174
1176protected:
1177 bool debug;
1181 uint16_t frameWidth;
1182 uint16_t frameHeight;
1186public:
1187 cFrameParser(void);
1188 virtual ~cFrameParser() {};
1189 virtual int Parse(const uchar *Data, int Length, int Pid) = 0;
1196 void SetDebug(bool Debug) { debug = Debug; }
1197 bool NewFrame(void) { return newFrame; }
1198 bool IndependentFrame(void) { return independentFrame; }
1200 uint16_t FrameWidth(void) { return frameWidth; }
1201 uint16_t FrameHeight(void) { return frameHeight; }
1202 double FramesPerSecond(void) { return framesPerSecond; }
1203 eScanType ScanType(void) { return scanType; }
1205 };
1206
1208{
1209 debug = true;
1210 newFrame = false;
1211 independentFrame = false;
1213 frameWidth = 0;
1214 frameHeight = 0;
1215 framesPerSecond = 0.0;
1218}
1219
1220// --- cAudioParser ----------------------------------------------------------
1221
1223public:
1224 cAudioParser(void);
1225 virtual int Parse(const uchar *Data, int Length, int Pid) override;
1226 };
1227
1231
1232int cAudioParser::Parse(const uchar *Data, int Length, int Pid)
1233{
1234 if (TsPayloadStart(Data)) {
1235 newFrame = independentFrame = true;
1236 if (debug)
1237 dbgframes("/");
1238 }
1239 else
1240 newFrame = independentFrame = false;
1241 return TS_SIZE;
1242}
1243
1244// --- cMpeg2Parser ----------------------------------------------------------
1245
1247private:
1248 uint32_t scanner;
1252 const double frame_rate_table[9] = {
1253 0, // 0 forbidden
1254 24000./1001., // 1 23.976...
1255 24., // 2 24
1256 25., // 3 25
1257 30000./1001., // 4 29.97...
1258 30., // 5 30
1259 50., // 6 50
1260 60000./1001., // 7 59.94...
1261 60. // 8 60
1262 };
1263public:
1264 cMpeg2Parser(void);
1265 virtual int Parse(const uchar *Data, int Length, int Pid) override;
1266 };
1267
1269{
1271 seenIndependentFrame = false;
1272 lastIFrameTemporalReference = -1; // invalid
1273 seenScanType = false;
1274}
1275
1276int cMpeg2Parser::Parse(const uchar *Data, int Length, int Pid)
1277{
1278 newFrame = independentFrame = false;
1279 bool SeenPayloadStart = false;
1280 cTsPayload tsPayload(const_cast<uchar *>(Data), Length, Pid);
1281 if (TsPayloadStart(Data)) {
1282 SeenPayloadStart = true;
1283 tsPayload.SkipPesHeader();
1286 dbgframes("/");
1287 }
1288 uint32_t OldScanner = scanner; // need to remember it in case of multiple frames per payload
1289 for (;;) {
1290 if (!SeenPayloadStart && tsPayload.AtTsStart())
1291 OldScanner = scanner;
1292 scanner = (scanner << 8) | tsPayload.GetByte();
1293 if (scanner == 0x00000100) { // Picture Start Code
1294 if (!SeenPayloadStart && tsPayload.GetLastIndex() > TS_SIZE) {
1295 scanner = OldScanner;
1296 return tsPayload.Used() - TS_SIZE;
1297 }
1298 uchar b1 = tsPayload.GetByte();
1299 uchar b2 = tsPayload.GetByte();
1300 int TemporalReference = (b1 << 2 ) + ((b2 & 0xC0) >> 6);
1301 uchar FrameType = (b2 >> 3) & 0x07;
1302 if (tsPayload.Find(0x000001B5)) { // Extension start code
1303 if (((tsPayload.GetByte() & 0xF0) >> 4) == 0x08) { // Picture coding extension
1304 tsPayload.GetByte();
1305 uchar PictureStructure = tsPayload.GetByte() & 0x03;
1306 if (PictureStructure == 0x02) // bottom field
1307 break;
1308 }
1309 }
1310 newFrame = true;
1311 independentFrame = FrameType == 1; // I-Frame
1312 if (independentFrame) {
1315 lastIFrameTemporalReference = TemporalReference;
1316 }
1317 if (debug) {
1320 static const char FrameTypes[] = "?IPBD???";
1321 dbgframes("%c", FrameTypes[FrameType]);
1322 }
1323 }
1324 tsPayload.Statistics();
1325 break;
1326 }
1327 else if (frameWidth == 0 && scanner == 0x000001B3) { // Sequence header code
1328 frameWidth = tsPayload.GetByte() << 4;
1329 uchar b = tsPayload.GetByte(); // ignoring two MSB of width and height in sequence extension
1330 frameWidth |= b >> 4; // as 12 Bit = max 4095 should be sufficient for all available MPEG2 streams
1331 frameHeight = (b & 0x0F) << 8 | tsPayload.GetByte();
1332 b = tsPayload.GetByte(); // hi: aspect ratio info, lo: frame rate code
1333 switch (b >> 4) {
1334 case 1: aspectRatio = ar_1_1; break;
1335 case 2: aspectRatio = ar_4_3; break;
1336 case 3: aspectRatio = ar_16_9; break;
1337 case 4: aspectRatio = ar_2_21_1; break;
1338 default: aspectRatio = arUnknown;
1339 }
1340 uchar frame_rate_value = b & 0x0F;
1341 if (frame_rate_value > 0 && frame_rate_value <= 8)
1342 framesPerSecond = frame_rate_table[frame_rate_value];
1343 }
1344 else if (!seenScanType && scanner == 0x000001B5) { // Extension start code
1345 if ((tsPayload.GetByte() & 0xF0) == 0x10) { // Sequence Extension
1346 scanType = (tsPayload.GetByte() & 0x40) ? stProgressive : stInterlaced;
1347 seenScanType = true;
1348 if (debug) {
1350 dsyslog("%s", *s);
1351 dbgframes("\n%s", *s);
1352 }
1353 }
1354 }
1355 if (tsPayload.AtPayloadStart() // stop at a new payload start to have the buffer refilled if necessary
1356 || tsPayload.Eof()) // or if we're out of data
1357 break;
1358 }
1359 return tsPayload.Used();
1360}
1361
1362// --- cH264Parser -----------------------------------------------------------
1363
1365private:
1372 uchar byte; // holds the current byte value in case of bitwise access
1373 int bit; // the bit index into the current byte (-1 if we're not in bit reading mode)
1374 int zeroBytes; // the number of consecutive zero bytes (to detect 0x000003)
1375 // Identifiers written in '_' notation as in "ITU-T H.264":
1379protected:
1381 uint32_t scanner;
1384 uchar GetByte(bool Raw = false);
1388 uchar GetBit(void);
1389 uint32_t GetBits(int Bits);
1390 uint32_t GetGolombUe(void);
1391 int32_t GetGolombSe(void);
1392 void ParseAccessUnitDelimiter(void);
1393 void ParseSequenceParameterSet(void);
1394 void ParseSliceHeader(void);
1395public:
1396 cH264Parser(void);
1400 virtual int Parse(const uchar *Data, int Length, int Pid) override;
1401 };
1402
1404{
1405 byte = 0;
1406 bit = -1;
1407 zeroBytes = 0;
1411 frame_mbs_only_flag = false;
1412 gotAccessUnitDelimiter = false;
1414}
1415
1417{
1418 uchar b = tsPayload.GetByte();
1419 if (!Raw) {
1420 // If we encounter the byte sequence 0x000003, we need to skip the 0x03:
1421 if (b == 0x00)
1422 zeroBytes++;
1423 else {
1424 if (b == 0x03 && zeroBytes >= 2)
1425 b = tsPayload.GetByte();
1426 zeroBytes = b ? 0 : 1;
1427 }
1428 }
1429 else
1430 zeroBytes = 0;
1431 bit = -1;
1432 return b;
1433}
1434
1436{
1437 if (bit < 0) {
1438 byte = GetByte();
1439 bit = 7;
1440 }
1441 return (byte & (1 << bit--)) ? 1 : 0;
1442}
1443
1444uint32_t cH264Parser::GetBits(int Bits)
1445{
1446 uint32_t b = 0;
1447 while (Bits--)
1448 b |= GetBit() << Bits;
1449 return b;
1450}
1451
1453{
1454 int z = -1;
1455 for (int b = 0; !b && z < 32; z++) // limiting z to no get stuck if GetBit() always returns 0
1456 b = GetBit();
1457 return (1 << z) - 1 + GetBits(z);
1458}
1459
1461{
1462 uint32_t v = GetGolombUe();
1463 if (v) {
1464 if ((v & 0x01) != 0)
1465 return (v + 1) / 2; // fails for v == 0xFFFFFFFF, but that will probably never happen
1466 else
1467 return -int32_t(v / 2);
1468 }
1469 return v;
1470}
1471
1472int cH264Parser::Parse(const uchar *Data, int Length, int Pid)
1473{
1474 newFrame = independentFrame = false;
1475 tsPayload.Setup(const_cast<uchar *>(Data), Length, Pid);
1476 if (TsPayloadStart(Data)) {
1477 tsPayload.SkipPesHeader();
1480 dbgframes("/");
1481 }
1482 }
1483 for (;;) {
1484 scanner = (scanner << 8) | GetByte(true);
1485 if ((scanner & 0xFFFFFF00) == 0x00000100) { // NAL unit start
1486 uchar NalUnitType = scanner & 0x1F;
1487 switch (NalUnitType) {
1490 break;
1494 }
1495 break;
1499 gotAccessUnitDelimiter = false;
1500 if (newFrame)
1501 tsPayload.Statistics();
1502 return tsPayload.Used();
1503 }
1504 break;
1505 default: ;
1506 }
1507 }
1508 if (tsPayload.AtPayloadStart() // stop at a new payload start to have the buffer refilled if necessary
1509 || tsPayload.Eof()) // or if we're out of data
1510 break;
1511 }
1512 return tsPayload.Used();
1513}
1514
1516{
1518 dbgframes("A");
1519 GetByte(); // primary_pic_type
1520}
1521
1523{
1524 int chroma_format_idc = 0;
1525 int bitDepth = 0;
1526 uchar profile_idc = GetByte(); // profile_idc
1527 GetByte(); // constraint_set[0-5]_flags, reserved_zero_2bits
1528 GetByte(); // level_idc
1529 GetGolombUe(); // seq_parameter_set_id
1530 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) {
1531 chroma_format_idc = GetGolombUe(); // chroma_format_idc
1532 if (chroma_format_idc == 3)
1534 bitDepth = 8 + GetGolombUe(); // bit_depth_luma_minus8
1535 GetGolombUe(); // bit_depth_chroma_minus8
1536 GetBit(); // qpprime_y_zero_transform_bypass_flag
1537 if (GetBit()) { // seq_scaling_matrix_present_flag
1538 for (int i = 0; i < ((chroma_format_idc != 3) ? 8 : 12); i++) {
1539 if (GetBit()) { // seq_scaling_list_present_flag
1540 int SizeOfScalingList = (i < 6) ? 16 : 64;
1541 int LastScale = 8;
1542 int NextScale = 8;
1543 for (int j = 0; j < SizeOfScalingList; j++) {
1544 if (NextScale)
1545 NextScale = (LastScale + GetGolombSe() + 256) % 256; // delta_scale
1546 if (NextScale)
1547 LastScale = NextScale;
1548 }
1549 }
1550 }
1551 }
1552 }
1553 log2_max_frame_num = GetGolombUe() + 4; // log2_max_frame_num_minus4
1554 int pic_order_cnt_type = GetGolombUe(); // pic_order_cnt_type
1555 if (pic_order_cnt_type == 0)
1556 GetGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
1557 else if (pic_order_cnt_type == 1) {
1558 GetBit(); // delta_pic_order_always_zero_flag
1559 GetGolombSe(); // offset_for_non_ref_pic
1560 GetGolombSe(); // offset_for_top_to_bottom_field
1561 for (int i = GetGolombUe(); i--; ) // num_ref_frames_in_pic_order_cnt_cycle
1562 GetGolombSe(); // offset_for_ref_frame
1563 }
1564 GetGolombUe(); // max_num_ref_frames
1565 GetBit(); // gaps_in_frame_num_value_allowed_flag
1566 uint16_t frame_Width = 16 * (1 + GetGolombUe()); // pic_width_in_mbs_minus1
1567 uint16_t frame_Height = 16 * (1 + GetGolombUe()); // pic_height_in_map_units_minus1
1568 frame_mbs_only_flag = GetBit(); // frame_mbs_only_flag
1569 if (frameWidth == 0) {
1571 if (!frame_mbs_only_flag) {
1572 GetBit(); // mb_adaptive_frame_field_flag
1573 frame_Height *= 2;
1574 }
1575 GetBit(); // direct_8x8_inference_flag
1576 bool frame_cropping_flag = GetBit(); // frame_cropping_flag
1577 if (frame_cropping_flag) {
1578 uint16_t frame_crop_left_offset = GetGolombUe(); // frame_crop_left_offset
1579 uint16_t frame_crop_right_offset = GetGolombUe(); // frame_crop_right_offset
1580 uint16_t frame_crop_top_offset = GetGolombUe(); // frame_crop_top_offset
1581 uint16_t frame_crop_bottom_offset = GetGolombUe(); // frame_crop_bottom_offset
1582 uint16_t CropUnitX = 1;
1583 uint16_t CropUnitY = frame_mbs_only_flag ? 1 : 2;
1584 if (!separate_colour_plane_flag && chroma_format_idc > 0) {
1585 if (chroma_format_idc == 1) {
1586 CropUnitX = 2;
1587 CropUnitY *= 2;
1588 }
1589 else if (chroma_format_idc == 2)
1590 CropUnitX = 2;
1591 }
1592 frame_Width -= CropUnitX * (frame_crop_left_offset + frame_crop_right_offset);
1593 frame_Height -= CropUnitY * (frame_crop_top_offset + frame_crop_bottom_offset);
1594 if (frame_Height > 1080 && frame_Height <= 1090) // workaround for channels with wrong crop parameters
1595 frame_Height = 1080;
1596 }
1597 frameWidth = frame_Width;
1598 frameHeight = frame_Height;
1599 // VUI parameters
1600 if (GetBit()) { // vui_parameters_present_flag
1601 if (GetBit()) { // aspect_ratio_info_present
1602 int aspect_ratio_idc = GetBits(8); // aspect_ratio_idc
1603 if (aspect_ratio_idc == 255) // EXTENDED_SAR
1604 GetBits(32); // sar_width, sar_height
1605 else if (frameHeight >= 720 && (aspect_ratio_idc == 1 || aspect_ratio_idc == 14 || aspect_ratio_idc == 15 || aspect_ratio_idc == 16))
1607 // implement decoding of other aspect_ratio_idc values when they are required
1608 }
1609 if (GetBit()) // overscan_info_present_flag
1610 GetBit(); // overscan_approriate_flag
1611 if (GetBit()) { // video_signal_type_present_flag
1612 GetBits(4); // video_format, video_full_range_flag
1613 if (GetBit()) // colour_description_present_flag
1614 GetBits(24); // colour_primaries, transfer_characteristics, matrix_coefficients
1615 }
1616 if (GetBit()) { // chroma_loc_info_present_flag
1617 GetGolombUe(); // chroma_sample_loc_type_top_field
1618 GetGolombUe(); // chroma_sample_loc_type_bottom_field
1619 }
1620 if (GetBit()) { // timing_info_present_flag
1621 uint32_t num_units_in_tick = GetBits(32); // num_units_in_tick
1622 uint32_t time_scale = GetBits(32); // time_scale
1623 if (num_units_in_tick > 0)
1624 framesPerSecond = double(time_scale) / (num_units_in_tick << 1);
1625 }
1626 }
1627 if (debug) {
1628 cString s = cString::sprintf("H.264: %d x %d%c %.2f fps %d Bit %s", frameWidth, frameHeight, ScanTypeChars[scanType], framesPerSecond, bitDepth, AspectRatioTexts[aspectRatio]);
1629 dsyslog("%s", *s);
1630 dbgframes("\n%s", *s);
1631 }
1632 }
1633 if (debug) {
1635 dbgframes("A"); // just for completeness
1636 dbgframes(frame_mbs_only_flag ? "S" : "s");
1637 }
1638}
1639
1641{
1642 newFrame = true;
1643 GetGolombUe(); // first_mb_in_slice
1644 int slice_type = GetGolombUe(); // slice_type, 0 = P, 1 = B, 2 = I, 3 = SP, 4 = SI
1645 independentFrame = (slice_type % 5) == 2;
1646 if (debug) {
1647 static const char SliceTypes[] = "PBIpi";
1648 dbgframes("%c", SliceTypes[slice_type % 5]);
1649 }
1651 return; // don't need the rest - a frame is complete
1652 GetGolombUe(); // pic_parameter_set_id
1654 GetBits(2); // colour_plane_id
1655 GetBits(log2_max_frame_num); // frame_num
1656 if (!frame_mbs_only_flag) {
1657 if (GetBit()) // field_pic_flag
1658 newFrame = !GetBit(); // bottom_field_flag
1659 if (debug)
1660 dbgframes(newFrame ? "t" : "b");
1661 }
1662}
1663
1664// --- cH265Parser -----------------------------------------------------------
1665
1704
1706:cH264Parser()
1707{
1708}
1709
1710int cH265Parser::Parse(const uchar *Data, int Length, int Pid)
1711{
1712 newFrame = independentFrame = false;
1713 tsPayload.Setup(const_cast<uchar *>(Data), Length, Pid);
1714 if (TsPayloadStart(Data)) {
1715 tsPayload.SkipPesHeader();
1717 }
1718 for (;;) {
1719 scanner = (scanner << 8) | GetByte(true);
1720 if ((scanner & 0xFFFFFF00) == 0x00000100) { // NAL unit start
1721 uchar NalUnitType = (scanner >> 1) & 0x3F;
1722 GetByte(); // nuh_layer_id + nuh_temporal_id_plus1
1723 if (NalUnitType <= nutSliceSegmentRASLR || (NalUnitType >= nutSliceSegmentBLAWLP && NalUnitType <= nutSliceSegmentCRANUT)) {
1724 if (NalUnitType == nutSliceSegmentIDRWRADL || NalUnitType == nutSliceSegmentIDRNLP || NalUnitType == nutSliceSegmentCRANUT)
1725 independentFrame = true;
1726 if (GetBit()) { // first_slice_segment_in_pic_flag
1727 newFrame = true;
1728 tsPayload.Statistics();
1729 }
1730 break;
1731 }
1732 else if (frameWidth == 0 && NalUnitType == nutSequenceParameterSet) {
1735 }
1736 }
1737 if (tsPayload.AtPayloadStart() // stop at a new payload start to have the buffer refilled if necessary
1738 || tsPayload.Eof()) // or if we're out of data
1739 break;
1740 }
1741 return tsPayload.Used();
1742}
1743
1745{
1747 uint8_t sub_layer_profile_present_flag[8];
1748 uint8_t sub_layer_level_present_flag[8];
1749 GetBits(4); // sps_video_parameter_set_id
1750 int sps_max_sub_layers_minus1 = GetBits(3) & 7; // sps_max_sub_layers_minus1 ("& 7" to silence a compiler warning with gcc 14.1.0)
1751 GetBit(); // sps_temporal_id_nesting_flag
1752 // begin profile_tier_level(1, sps_max_sub_layers_minus1)
1753 GetByte();
1754 GetByte();
1755 GetByte();
1756 GetByte();
1757 GetByte();
1758 bool general_progressive_source_flag = GetBit(); // general_progressive_source_flag
1759 scanType = general_progressive_source_flag ? stProgressive : stInterlaced;
1760 GetBit(); // general_interlaced_source_flag
1761 GetBits(6);
1762 GetByte();
1763 GetByte();
1764 GetByte();
1765 GetByte();
1766 GetByte();
1767 GetByte(); // general_level_idc
1768 for (int i = 0; i < sps_max_sub_layers_minus1; i++ ) {
1769 sub_layer_profile_present_flag[i] = GetBit(); // sub_layer_profile_present_flag[i]
1770 sub_layer_level_present_flag[i] = GetBit(); // sub_layer_level_present_flag[i]
1771 }
1772 if (sps_max_sub_layers_minus1 > 0) {
1773 for (int i = sps_max_sub_layers_minus1; i < 8; i++ )
1774 GetBits(2); // reserved_zero_2bits[i]
1775 }
1776 for (int i = 0; i < sps_max_sub_layers_minus1; i++ ) {
1777 if (sub_layer_profile_present_flag[i] )
1778 GetBits(88);
1779 if (sub_layer_level_present_flag[i])
1780 GetBits(8);
1781 }
1782 // end profile_tier_level
1783 GetGolombUe(); // sps_seq_parameter_set_id
1784 int chroma_format_idc = GetGolombUe(); // chroma_format_idc
1785 if (chroma_format_idc == 3)
1786 separate_colour_plane_flag = GetBit(); // separate_colour_plane_flag
1787 frameWidth = GetGolombUe(); // pic_width_in_luma_samples
1788 frameHeight = GetGolombUe(); // pic_height_in_luma_samples
1789 bool conformance_window_flag = GetBit(); // conformance_window_flag
1790 if (conformance_window_flag) {
1791 int conf_win_left_offset = GetGolombUe(); // conf_win_left_offset
1792 int conf_win_right_offset = GetGolombUe(); // conf_win_right_offset
1793 int conf_win_top_offset = GetGolombUe(); // conf_win_top_offset
1794 int conf_win_bottom_offset = GetGolombUe(); // conf_win_bottom_offset
1795 uint16_t SubWidthC = 1;
1796 uint16_t SubHeightC = 1;
1797 if (!separate_colour_plane_flag && chroma_format_idc > 0) {
1798 if (chroma_format_idc == 1) {
1799 SubWidthC = 2;
1800 SubHeightC = 2;
1801 }
1802 else if (chroma_format_idc == 2)
1803 SubWidthC = 2;
1804 }
1805 frameWidth -= SubWidthC * (conf_win_left_offset + conf_win_right_offset);
1806 frameHeight -= SubHeightC * (conf_win_top_offset + conf_win_bottom_offset);
1807 }
1808 int bitDepth = 8 + GetGolombUe(); // bit_depth_luma_minus8
1809 GetGolombUe(); // bit_depth_chroma_minus8
1810 int log2_max_pic_order_cnt_lsb_minus4 = GetGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
1811 int sps_sub_layer_ordering_info_present_flag = GetBit(); // sps_sub_layer_ordering_info_present_flag
1812 for (int i = sps_sub_layer_ordering_info_present_flag ? 0 : sps_max_sub_layers_minus1; i <= sps_max_sub_layers_minus1; ++i) {
1813 GetGolombUe(); // sps_max_dec_pic_buffering_minus1[i]
1814 GetGolombUe(); // sps_max_num_reorder_pics[i]
1815 GetGolombUe(); // sps_max_latency_increase_plus1[i]
1816 }
1817 GetGolombUe(); // log2_min_luma_coding_block_size_minus3
1818 GetGolombUe(); // log2_diff_max_min_luma_coding_block_size
1819 GetGolombUe(); // log2_min_luma_transform_block_size_minus2
1820 GetGolombUe(); // log2_diff_max_min_luma_transform_block_size
1821 GetGolombUe(); // max_transform_hierarchy_depth_inter
1822 GetGolombUe(); // max_transform_hierarchy_depth_intra
1823 if (GetBit()) { // scaling_list_enabled_flag
1824 if (GetBit()) { // sps_scaling_list_data_present_flag
1825 // begin scaling_list_data
1826 for (int sizeId = 0; sizeId < 4; ++sizeId) {
1827 for (int matrixId = 0; matrixId < 6; matrixId += (sizeId == 3) ? 3 : 1) {
1828 if (!GetBit()) // scaling_list_pred_mode_flag[sizeId][matrixId]
1829 GetGolombUe(); // scaling_list_pred_matrix_id_delta[sizeId][matrixId]
1830 else {
1831 int coefNum = min(64, (1 << (4 + (sizeId << 1))));
1832 if (sizeId > 1)
1833 GetGolombSe(); // scaling_list_dc_coef_minus8[sizeId−2][matrixId]
1834 for (int i = 0; i < coefNum; ++i)
1835 GetGolombSe(); // scaling_list_delta_coef
1836 }
1837 }
1838 }
1839 }
1840 // end scaling_list_data
1841 }
1842 GetBits(2); // amp_enabled_flag, sample_adaptive_offset_enabled_flag
1843 if (GetBit()) { // pcm_enabled_flag
1844 GetBits(8); // pcm_sample_bit_depth_luma_minus1, pcm_sample_bit_depth_chroma_minus1
1845 GetGolombUe(); // log2_min_pcm_luma_coding_block_size_minus3
1846 GetGolombUe(); // log2_diff_max_min_pcm_luma_coding_block_size
1847 GetBit(); // pcm_loop_filter_disabled_flag
1848 }
1849 uint32_t num_short_term_ref_pic_sets = GetGolombUe(); // num_short_term_ref_pic_sets
1850 uint32_t NumDeltaPocs[num_short_term_ref_pic_sets];
1851 for (uint32_t stRpsIdx = 0; stRpsIdx < num_short_term_ref_pic_sets; ++stRpsIdx) {
1852 // start of st_ref_pic_set(stRpsIdx)
1853 bool inter_ref_pic_set_prediction_flag = false;
1854 if (stRpsIdx != 0)
1855 inter_ref_pic_set_prediction_flag = GetBit(); // inter_ref_pic_set_prediction_flag
1856 if (inter_ref_pic_set_prediction_flag) {
1857 uint32_t RefRpsIdx, delta_idx_minus1 = 0;
1858 if (stRpsIdx == num_short_term_ref_pic_sets)
1859 delta_idx_minus1 = GetGolombUe(); // delta_idx_minus1
1860 GetBit(); // delta_rps_sign
1861 GetGolombUe(); // abs_delta_rps_minus1
1862 RefRpsIdx = stRpsIdx - (delta_idx_minus1 + 1);
1863 NumDeltaPocs[stRpsIdx] = 0;
1864 for (uint32_t j = 0; j <= NumDeltaPocs[RefRpsIdx]; ++j) {
1865 if (!GetBit()) { // used_by_curr_pic_flag[j]
1866 if (GetBit()) // use_delta_flag[j]
1867 NumDeltaPocs[stRpsIdx]++;
1868 }
1869 else
1870 NumDeltaPocs[stRpsIdx]++;
1871 }
1872 }
1873 else {
1874 uint32_t num_negative_pics = GetGolombUe(); // num_negative_pics
1875 uint32_t num_positive_pics = GetGolombUe(); // num_positive_pics
1876 for (uint32_t j = 0; j < num_negative_pics; ++j) {
1877 GetGolombUe(); // delta_poc_s0_minus1[i]
1878 GetBit(); // used_by_curr_pic_s0_flag[i]
1879 }
1880 for (uint32_t j = 0; j < num_positive_pics; ++j) {
1881 GetGolombUe(); // delta_poc_s1_minus1[i]
1882 GetBit(); // delta_poc_s1_minus1[i]
1883 }
1884 NumDeltaPocs[stRpsIdx] = num_negative_pics + num_positive_pics;
1885 }
1886 // end of st_ref_pic_set(stRpsIdx)
1887 }
1888 if (GetBit()) { // long_term_ref_pics_present_flag
1889 uint32_t num_long_term_ref_pics_sps = GetGolombUe(); // num_long_term_ref_pics_sps
1890 for (uint32_t i = 0; i < num_long_term_ref_pics_sps; ++i) {
1891 GetBits(log2_max_pic_order_cnt_lsb_minus4 + 4); // lt_ref_pic_poc_lsb_sps[i]
1892 GetBit(); // used_by_curr_pic_lt_sps_flag[i]
1893 }
1894 }
1895 GetBits(2); // sps_temporal_mvp_enabled_flag, strong_intra_smoothing_enabled_flag
1896 if (GetBit()) { // vui_parameters_present_flag
1897 // begin of vui_parameters()
1898 if (GetBit()) { // aspect_ratio_info_present_flag
1899 int aspect_ratio_idc = GetBits(8); // aspect_ratio_idc
1900 if (aspect_ratio_idc == 255) // EXTENDED_SAR
1901 GetBits(32); // sar_width, sar_height
1902 else if (aspect_ratio_idc == 1 || aspect_ratio_idc == 14)
1904 // implement decoding of other aspect_ratio_idc values when they are required
1905 }
1906 if (GetBit()) // overscan_info_present_flag
1907 GetBit(); // overscan_appropriate_flag
1908 if (GetBit()) { // video_signal_type_present_flag
1909 GetBits(4); // video_format, video_full_range_flag
1910 if (GetBit()) // colour_description_present_flag
1911 GetBits(24); // colour_primaries, transfer_characteristics, matrix_coeffs
1912 }
1913 if (GetBit()) { // chroma_loc_info_present_flag
1914 GetGolombUe(); // chroma_sample_loc_type_top_field
1915 GetGolombUe(); // chroma_sample_loc_type_bottom_field
1916 }
1917 GetBits(3); // neutral_chroma_indication_flag, field_seq_flag, frame_field_info_present_flag
1918 if (GetBit()) { // default_display_window_flag
1919 GetGolombUe(); // def_disp_win_left_offset
1920 GetGolombUe(); // def_disp_win_right_offset
1921 GetGolombUe(); // def_disp_win_top_offset
1922 GetGolombUe(); // def_disp_win_bottom_offset
1923 }
1924 if (GetBit()) { // vui_timing_info_present_flag
1925 uint32_t vui_num_units_in_tick = GetBits(32); // vui_num_units_in_tick
1926 uint32_t vui_time_scale = GetBits(32); // vui_time_scale
1927 if (vui_num_units_in_tick > 0)
1928 framesPerSecond = (double)vui_time_scale / vui_num_units_in_tick;
1929 }
1930 }
1931 if (debug) {
1932 cString s = cString::sprintf("H.265: %d x %d%c %.2f fps %d Bit %s", frameWidth, frameHeight, ScanTypeChars[scanType], framesPerSecond, bitDepth, AspectRatioTexts[aspectRatio]);
1933 dsyslog("%s", *s);
1934 dbgframes("\n%s", *s);
1935 }
1936}
1937
1938static bool DebugChecks = false;
1939
1940// cTsChecker and cPtsChecker are used to detect errors in the recorded data stream.
1941// While cTsChecker checks the continuity counter of the incoming TS packets, cPtsChecker
1942// works on entire frames, checking their PTS (Presentation Time Stamps) to see whether
1943// all expected frames arrive. The resulting number of errors is not a precise value.
1944// If it is zero, the recording can be safely considered error free. The higher the value,
1945// the more damaged the recording is.
1946
1947// --- cTsChecker ------------------------------------------------------------
1948
1949#define TS_CC_UNKNOWN 0xFF
1950
1952private:
1956 void Report(int Pid, const char *Message);
1957public:
1958 cTsChecker(void);
1959 void Reset(void);
1960 void Clear(void);
1961 int Errors(void) { return errors; }
1962 bool NewErrors(void);
1963 void CheckTs(const uchar *Data, int Length);
1964 };
1965
1967{
1968 Reset();
1969 Clear();
1970}
1971
1973{
1974 memset(counter, TS_CC_UNKNOWN, sizeof(counter));
1975}
1976
1978{
1979 errors = 0;
1980 oldErrors = 0;
1981}
1982
1983void cTsChecker::Report(int Pid, const char *Message)
1984{
1985 errors++;
1986 if (DebugChecks)
1987 fprintf(stderr, "%s: TS error #%d on PID %d (%s)\n", *TimeToString(time(NULL)), errors, Pid, Message);
1988}
1989
1991{
1992 bool e = oldErrors < errors;
1993 oldErrors = errors;
1994 return e;
1995}
1996
1997void cTsChecker::CheckTs(const uchar *Data, int Length)
1998{
1999 while (Length >= TS_SIZE) {
2000 int Pid = TsPid(Data);
2001 uchar Cc = TsContinuityCounter(Data);
2002 if (TsHasPayload(Data)) {
2003 if (TsError(Data))
2004 Report(Pid, "tei");
2005 else if (TsIsScrambled(Data))
2006 Report(Pid, "scrambled");
2007 else {
2008 uchar OldCc = counter[Pid];
2009 if (OldCc != TS_CC_UNKNOWN) {
2010 uchar NewCc = (OldCc + 1) & TS_CONT_CNT_MASK;
2011 if (Cc != NewCc)
2012 Report(Pid, "continuity");
2013 }
2014 }
2015 }
2016 counter[Pid] = Cc;
2017 Data += TS_SIZE;
2018 Length -= TS_SIZE;
2019 }
2020}
2021
2022// --- cPtsChecker -----------------------------------------------------------
2023
2024static inline int ComparePts(const void *a, const void *b)
2025{
2026 return PtsDiff(*(const int64_t *)b, *(const int64_t *)a);
2027}
2029private:
2031 int64_t lastPts;
2036public:
2037 cPtsChecker(void);
2038 void Clear(void);
2039 void SetFrameDelta(int FrameDelta) { frameDelta = FrameDelta; }
2040 void Process(void);
2041 void AddPts(int64_t Pts, bool IndependentFrame = false);
2042 bool NewMissing(void);
2043 int Missing(void);
2044 };
2045
2047{
2048 frameDelta = 0;
2049 Clear();
2050}
2051
2053{
2054 lastPts = -1;
2055 iFrameNoPts = false;
2056 totalMissing = 0;
2057 oldMissing = 0;
2058}
2059
2061{
2062 if (pts.Size() > 1) {
2063 pts.Sort(ComparePts);
2064 if (frameDelta == 0) {
2065 int Delta = INT_MAX;
2066 for (int i = 1; i < pts.Size(); i++) {
2067 int d = int(PtsDiff(pts[i - 1], pts[i]));
2068 if (d > 0 && d < Delta)
2069 Delta = d;
2070 }
2071 if (Delta < INT_MAX)
2072 frameDelta = Delta;
2073 }
2074 if (frameDelta == 0)
2075 return; // can't continue without frameDelta
2076 int Missing = 0;
2077 int Number = pts.Size();
2078 if (Number > 0 && pts[Number - 1] == lastPts) {
2079 // Don't get stuck at a total discontinuity:
2080 pts.Remove(Number - 1);
2081 Number--;
2082 }
2083 for (int i = 1; i < Number; i++) {
2084 int d = int(PtsDiff(pts[i - 1], pts[i]));
2085 if (d > frameDelta) {
2086 d = (d / frameDelta) - 1;
2087 if (d > 0 && iFrameNoPts) {
2088 d--;
2089 iFrameNoPts = false;
2090 }
2091 if (d > 0)
2092 Missing += d;
2093 }
2094 }
2096 pts.Remove(0, Number - 1); // leave the last value in the list
2097 if (pts.Size() == 1)
2098 lastPts = pts[0];
2099 }
2100}
2101
2102void cPtsChecker::AddPts(int64_t Pts, bool IndependentFrame)
2103{
2104 if (IndependentFrame) {
2105 Process();
2106 // In H.265 there can be I-frames that have no PTS (if anybody knows how players
2107 // handle this, please let me know). This is a workaround for such cases:
2108 if (Pts < 0)
2109 iFrameNoPts = true;
2110 }
2111 if (Pts >= 0)
2112 pts.Append(Pts);
2113}
2114
2116{
2117 bool m = oldMissing < totalMissing;
2119 return m;
2120}
2121
2123{
2124 return totalMissing;
2125}
2126
2127// --- cFrameChecker ---------------------------------------------------------
2128
2134
2136{
2137 delete ptsChecker;
2138 delete tsChecker;
2139}
2140
2142{
2143 tsChecker->Reset();
2144}
2145
2146bool cFrameChecker::Check(const uchar *Data, int Length, bool Independent, bool &Errors, bool &Missing, bool Final, int Pid)
2147{
2148 tsChecker->CheckTs(Data, Length);
2149 Errors = tsChecker->NewErrors();
2150 ptsChecker->AddPts(TsGetPts(Data, Length, Pid), Independent);
2151 if (Final)
2152 ptsChecker->Process();
2153 Missing = ptsChecker->NewMissing();
2154 return Errors || Missing;
2155}
2156
2158{
2159 return tsChecker->Errors() + ptsChecker->Missing();
2160}
2161
2162// --- cFrameDetector --------------------------------------------------------
2163
2164const char *ScanTypeChars = "-pi"; // index is eScanType
2165const char *AspectRatioTexts[] = { // index is eAspectRatio
2166 "-",
2167 "1:1",
2168 "4:3",
2169 "16:9",
2170 "2.21:1",
2171 NULL
2172 };
2173
2175{
2176 parser = NULL;
2177 tsChecker = new cTsChecker;
2178 ptsChecker = new cPtsChecker; // must be done before calling SetPid()
2179 SetPid(Pid, Type);
2180 synced = false;
2181 newFrame = independentFrame = false;
2182 numPtsValues = 0;
2183 numIFrames = 0;
2184 framesPerSecond = 0;
2185 frameWidth = 0;
2186 frameHeight = 0;
2190 scanning = false;
2191 firstIframeSeen = false;
2192 previousErrors = 0;
2193 missingFrames = 0;
2194}
2195
2197{
2198 delete ptsChecker;
2199 delete tsChecker;
2200}
2201
2202static int CmpUint32(const void *p1, const void *p2)
2203{
2204 if (*(uint32_t *)p1 < *(uint32_t *)p2) return -1;
2205 if (*(uint32_t *)p1 > *(uint32_t *)p2) return 1;
2206 return 0;
2207}
2208
2209void cFrameDetector::SetPid(int Pid, int Type)
2210{
2211 pid = Pid;
2212 type = Type;
2213 isVideo = type == 0x01 || type == 0x02 || type == 0x1B || type == 0x24; // MPEG 1, 2, H.264 or H.265
2214 delete parser;
2215 parser = NULL;
2216 if (type == 0x01 || type == 0x02)
2217 parser = new cMpeg2Parser;
2218 else if (type == 0x1B)
2219 parser = new cH264Parser;
2220 else if (type == 0x24)
2221 parser = new cH265Parser;
2222 else if (type == 0x03 || type == 0x04 || type == 0x06) // MPEG audio or AC3 audio
2223 parser = new cAudioParser;
2224 else if (type != 0)
2225 esyslog("ERROR: unknown stream type %d (PID %d) in frame detector", type, pid);
2226 tsChecker->Reset();
2227}
2228
2229void cFrameDetector::SetLastPts(int64_t LastPts)
2230{
2231 ptsChecker->AddPts(LastPts);
2232 ptsChecker->Clear();
2233}
2234
2235int cFrameDetector::Errors(bool *PreviousErrors, bool *MissingFrames)
2236{
2237 if (PreviousErrors)
2238 *PreviousErrors = tsChecker->NewErrors();
2239 if (MissingFrames) {
2240 ptsChecker->Process();
2241 *MissingFrames = ptsChecker->NewMissing();
2242 }
2243 return tsChecker->Errors() + ptsChecker->Missing();
2244}
2245
2246bool cFrameDetector::NewFrame(int *PreviousErrors, int *MissingFrames)
2247{
2248 // This function is deprecated, PreviousErrors and MissingFrames only return 0 or 1, not actual numbers!
2249 if (newFrame) {
2250 if (PreviousErrors)
2251 *PreviousErrors = previousErrors;
2252 if (MissingFrames)
2253 *MissingFrames = missingFrames;
2254 }
2255 return newFrame;
2256}
2257
2258bool cFrameDetector::NewFrame(bool &PreviousErrors, bool &MissingFrames)
2259{
2260 if (newFrame) {
2261 PreviousErrors = previousErrors;
2262 MissingFrames = missingFrames;
2263 }
2264 return newFrame;
2265}
2266
2267int cFrameDetector::Analyze(const uchar *Data, int Length, bool ErrorCheck)
2268{
2269 if (!parser)
2270 return 0;
2271 int Processed = 0;
2272 newFrame = independentFrame = false;
2273 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
2274 // Sync on TS packet borders:
2275 if (int Skipped = TS_SYNC(Data, Length))
2276 return Processed + Skipped;
2277 // Handle at least one TS packet:
2278 int Handled = TS_SIZE;
2279 if (TsHasPayload(Data) && !TsIsScrambled(Data)) {
2280 int Pid = TsPid(Data);
2281 if (Pid == pid) {
2282 if (Processed)
2283 return Processed;
2284 if (TsPayloadStart(Data))
2285 scanning = true;
2286 if (scanning) {
2287 // Detect the beginning of a new frame:
2288 if (TsPayloadStart(Data)) {
2291 }
2292 int n = parser->Parse(Data, Length, pid);
2293 if (n > 0) {
2294 if (parser->NewFrame()) {
2295 newFrame = true;
2296 independentFrame = parser->IndependentFrame();
2298 if (synced) {
2299 if (framesPerPayloadUnit <= 1)
2300 scanning = false;
2301 }
2302 else {
2303 if (parser->FramesPerSecond() > 0.0) {
2304 framesPerSecond = parser->FramesPerSecond();
2305 frameWidth = parser->FrameWidth();
2306 frameHeight = parser->FrameHeight();
2307 scanType = parser->ScanType();
2308 aspectRatio = parser->AspectRatio();
2309 ptsChecker->SetFrameDelta(PTSTICKS / framesPerSecond);
2310 synced = true;
2311 parser->SetDebug(false);
2312 }
2314 if (independentFrame)
2315 numIFrames++;
2316 }
2317 if (synced && firstIframeSeen && ErrorCheck)
2318 ptsChecker->AddPts(TsGetPts(Data, n), independentFrame);
2319 }
2320 Handled = n;
2321 }
2322 }
2323 if (TsPayloadStart(Data)) {
2324 // Determine the frame rate from the PTS values in the PES headers:
2325 if (framesPerSecond <= 0.0) {
2326 // frame rate unknown, so collect a sequence of PTS values:
2327 if (numPtsValues < 2 || numPtsValues < MaxPtsValues && numIFrames < 2) { // collect a sequence containing at least two I-frames
2328 if (newFrame) { // only take PTS values at the beginning of a frame (in case of fields!)
2329 const uchar *Pes = Data + TsPayloadOffset(Data);
2330 if (numIFrames && PesHasPts(Pes)) {
2332 // check for rollover:
2333 if (numPtsValues && ptsValues[numPtsValues - 1] > 0xF0000000 && ptsValues[numPtsValues] < 0x10000000) {
2334 dbgframes("#");
2335 numPtsValues = 0;
2336 numIFrames = 0;
2337 }
2338 else
2339 numPtsValues++;
2340 }
2341 }
2342 }
2343 if (numPtsValues >= 2 && numIFrames >= 2) {
2344 // find the smallest PTS delta:
2345 qsort(ptsValues, numPtsValues, sizeof(uint32_t), CmpUint32);
2346 numPtsValues--;
2347 for (int i = 0; i < numPtsValues; i++)
2348 ptsValues[i] = ptsValues[i + 1] - ptsValues[i];
2349 qsort(ptsValues, numPtsValues, sizeof(uint32_t), CmpUint32);
2350 int Div = framesPerPayloadUnit;
2351 if (framesPerPayloadUnit > 1)
2352 Div += parser->IFrameTemporalReferenceOffset();
2353 if (Div <= 0)
2354 Div = 1;
2355 int Delta = ptsValues[0] / Div;
2356 // determine frame info:
2357 if (isVideo) {
2358 if (Delta == 3753)
2359 framesPerSecond = 24.0 / 1.001;
2360 else if (abs(Delta - 3600) <= 1)
2361 framesPerSecond = 25.0;
2362 else if (Delta % 3003 == 0)
2363 framesPerSecond = 30.0 / 1.001;
2364 else if (abs(Delta - 1800) <= 1)
2365 framesPerSecond = 50.0;
2366 else if (Delta == 1501)
2367 framesPerSecond = 60.0 / 1.001;
2368 else {
2370 dsyslog("unknown frame delta (%d), assuming %5.2f fps", Delta, framesPerSecond);
2371 }
2372 }
2373 else // audio
2374 framesPerSecond = double(PTSTICKS) / Delta; // PTS of audio frames is always increasing
2375 ptsChecker->SetFrameDelta(PTSTICKS / framesPerSecond);
2376 dbgframes("\nDelta = %d FPS = %5.2f FPPU = %d NF = %d TRO = %d\n", Delta, framesPerSecond, framesPerPayloadUnit, numPtsValues + 1, parser->IFrameTemporalReferenceOffset());
2377 synced = true;
2378 parser->SetDebug(false);
2379 }
2380 }
2381 }
2382 }
2383 else if (Pid == PATPID && synced && Processed)
2384 return Processed; // allow the caller to see any PAT packets
2385 }
2386 if (synced && firstIframeSeen && ErrorCheck) {
2387 if (newFrame) {
2388 previousErrors = tsChecker->NewErrors();
2389 missingFrames = ptsChecker->NewMissing();
2390 }
2391 tsChecker->CheckTs(Data, Handled);
2392 }
2393 Data += Handled;
2394 Length -= Handled;
2395 Processed += Handled;
2396 if (newFrame)
2397 break;
2398 }
2399 return Processed;
2400}
#define MAXDPIDS
Definition channels.h:32
#define MAXAPIDS
Definition channels.h:31
#define MAXSPIDS
Definition channels.h:33
#define MAXLANGCODE1
Definition channels.h:36
static u_int32_t crc32(const char *d, int len, u_int32_t CRCvalue)
Definition util.c:267
bool CheckCRCAndParse()
Definition si.c:65
Descriptor * getNext(Iterator &it)
Definition si.c:112
DescriptorTag getDescriptorTag() const
Definition si.c:100
StructureLoop< Language > languageLoop
Definition descriptor.h:490
bool getCurrentNextIndicator() const
Definition si.c:80
int getSectionNumber() const
Definition si.c:88
int getLastSectionNumber() const
Definition si.c:92
int getVersionNumber() const
Definition si.c:84
int getPid() const
Definition section.c:34
int getServiceId() const
Definition section.c:30
bool isNITPid() const
Definition section.h:31
StructureLoop< Association > associationLoop
Definition section.h:39
int getTransportStreamId() const
Definition section.c:26
DescriptorLoop streamDescriptors
Definition section.h:63
int getPid() const
Definition section.c:65
int getStreamType() const
Definition section.c:69
int getServiceId() const
Definition section.c:57
int getPCRPid() const
Definition section.c:61
StructureLoop< Stream > streamLoop
Definition section.h:71
StructureLoop< Subtitling > subtitlingLoop
Definition descriptor.h:332
virtual int Parse(const uchar *Data, int Length, int Pid) override
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition remux.c:1232
cAudioParser(void)
Definition remux.c:1228
const int * Dpids(void) const
Definition channels.h:158
uint16_t AncillaryPageId(int i) const
Definition channels.h:170
uint16_t CompositionPageId(int i) const
Definition channels.h:169
int Tpid(void) const
Definition channels.h:171
const char * Slang(int i) const
Definition channels.h:165
int Vpid(void) const
Definition channels.h:154
int Atype(int i) const
Definition channels.h:166
int Dtype(int i) const
Definition channels.h:167
int Dpid(int i) const
Definition channels.h:161
int Vtype(void) const
Definition channels.h:156
int Apid(int i) const
Definition channels.h:160
int Ppid(void) const
Definition channels.h:155
uchar SubtitlingType(int i) const
Definition channels.h:168
const char * Dlang(int i) const
Definition channels.h:164
int Spid(int i) const
Definition channels.h:162
const int * Apids(void) const
Definition channels.h:157
const char * Alang(int i) const
Definition channels.h:163
const int * Spids(void) const
Definition channels.h:159
static cDevice * PrimaryDevice(void)
Returns the primary device.
Definition device.h:148
void EnsureAudioTrack(bool Force=false)
Makes sure an audio track is selected that is actually available.
Definition device.c:1227
void ClrAvailableTracks(bool DescriptionsOnly=false, bool IdsOnly=false)
Clears the list of currently available tracks.
Definition device.c:1091
void EnsureSubtitleTrack(void)
Makes sure one of the preferred language subtitle tracks is selected.
Definition device.c:1260
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:1114
uchar eit[TS_SIZE]
Definition remux.h:434
cEitGenerator(int Sid=0)
Definition remux.c:951
int counter
Definition remux.h:435
uchar * AddParentalRatingDescriptor(uchar *p, uchar ParentalRating=0)
Definition remux.c:965
uint16_t YMDtoMJD(int Y, int M, int D)
Definition remux.c:959
uchar * Generate(int Sid)
Definition remux.c:976
int version
Definition remux.h:436
void Reset(void)
Definition remux.c:2141
cTsChecker * tsChecker
Definition remux.h:531
cFrameChecker(void)
Definition remux.c:2129
cPtsChecker * ptsChecker
Definition remux.h:532
int TotalErrors(void)
Returns the total number of all errors and missing frames detected in the data given to the calls to ...
Definition remux.c:2157
bool Check(const uchar *Data, int Length, bool Independent, bool &Errors, bool &Missing, bool Final, int Pid)
Check Length bytes of the given Data (which must be a complete frame), with Independent telling wheth...
Definition remux.c:2146
int previousErrors
Definition remux.h:572
uint32_t ptsValues[MaxPtsValues]
Definition remux.h:558
bool synced
Definition remux.h:555
uint16_t frameHeight
Definition remux.h:564
int Errors(bool *PreviousErrors=NULL, bool *MissingFrames=NULL)
Returns the total number of errors so far.
Definition remux.c:2235
cTsChecker * tsChecker
Definition remux.h:575
uint16_t frameWidth
Definition remux.h:563
bool scanning
Definition remux.h:570
int framesPerPayloadUnit
Definition remux.h:568
bool NewFrame(int *PreviousErrors=NULL, int *MissingFrames=NULL)
Definition remux.c:2246
bool firstIframeSeen
Definition remux.h:571
int Analyze(const uchar *Data, int Length, bool ErrorCheck=true)
Analyzes the TS packets pointed to by Data.
Definition remux.c:2267
double framesPerSecond
Definition remux.h:562
int framesInPayloadUnit
Definition remux.h:567
int numIFrames
Definition remux.h:560
cPtsChecker * ptsChecker
Definition remux.h:576
bool independentFrame
Definition remux.h:557
cFrameDetector(int Pid=0, int Type=0)
Sets up a frame detector for the given Pid and stream Type.
Definition remux.c:2174
eScanType scanType
Definition remux.h:565
int missingFrames
Definition remux.h:573
void SetLastPts(int64_t LastPts)
If this is a resumed recording, call this function with the last PTS of the previous recording.
Definition remux.c:2229
bool newFrame
Definition remux.h:556
cFrameParser * parser
Definition remux.h:574
int numPtsValues
Definition remux.h:559
bool isVideo
Definition remux.h:561
void SetPid(int Pid, int Type)
Sets the Pid and stream Type to detect frames for.
Definition remux.c:2209
eAspectRatio aspectRatio
Definition remux.h:566
uint16_t FrameWidth(void)
Definition remux.c:1200
void SetDebug(bool Debug)
Definition remux.c:1196
bool NewFrame(void)
Definition remux.c:1197
eAspectRatio aspectRatio
Definition remux.c:1185
double FramesPerSecond(void)
Definition remux.c:1202
bool IndependentFrame(void)
Definition remux.c:1198
bool newFrame
Definition remux.c:1178
int iFrameTemporalReferenceOffset
Definition remux.c:1180
double framesPerSecond
Definition remux.c:1183
eScanType ScanType(void)
Definition remux.c:1203
bool independentFrame
Definition remux.c:1179
virtual ~cFrameParser()
Definition remux.c:1188
uint16_t frameHeight
Definition remux.c:1182
bool debug
Definition remux.c:1177
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.
eAspectRatio AspectRatio(void)
Definition remux.c:1204
eScanType scanType
Definition remux.c:1184
uint16_t FrameHeight(void)
Definition remux.c:1201
int IFrameTemporalReferenceOffset(void)
Definition remux.c:1199
uint16_t frameWidth
Definition remux.c:1181
cFrameParser(void)
Definition remux.c:1207
uint32_t GetBits(int Bits)
Definition remux.c:1444
cTsPayload tsPayload
Definition remux.c:1380
void ParseAccessUnitDelimiter(void)
Definition remux.c:1515
@ nutSequenceParameterSet
Definition remux.c:1369
@ nutCodedSliceNonIdr
Definition remux.c:1367
@ nutAccessUnitDelimiter
Definition remux.c:1370
@ nutCodedSliceIdr
Definition remux.c:1368
bool separate_colour_plane_flag
Definition remux.c:1376
bool gotAccessUnitDelimiter
Definition remux.c:1382
int zeroBytes
Definition remux.c:1374
bool frame_mbs_only_flag
Definition remux.c:1378
int log2_max_frame_num
Definition remux.c:1377
virtual int Parse(const uchar *Data, int Length, int Pid) override
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition remux.c:1472
uint32_t GetGolombUe(void)
Definition remux.c:1452
uchar GetByte(bool Raw=false)
Gets the next data byte.
Definition remux.c:1416
void ParseSliceHeader(void)
Definition remux.c:1640
void ParseSequenceParameterSet(void)
Definition remux.c:1522
uchar byte
Definition remux.c:1372
uint32_t scanner
Definition remux.c:1381
bool gotSequenceParameterSet
Definition remux.c:1383
int32_t GetGolombSe(void)
Definition remux.c:1460
cH264Parser(void)
Sets up a new H.264 parser.
Definition remux.c:1403
uchar GetBit(void)
Definition remux.c:1435
virtual int Parse(const uchar *Data, int Length, int Pid) override
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition remux.c:1710
cH265Parser(void)
Definition remux.c:1705
@ nutSliceSegmentIDRWRADL
Definition remux.c:1682
@ nutUnspecified0
Definition remux.c:1696
@ nutSliceSegmentSTSAN
Definition remux.c:1673
@ nutSliceSegmentRADLN
Definition remux.c:1675
@ nutSliceSegmentIDRNLP
Definition remux.c:1683
@ nutPrefixSEI
Definition remux.c:1692
@ nutAccessUnitDelimiter
Definition remux.c:1688
@ nutUnspecified7
Definition remux.c:1697
@ nutSliceSegmentBLAWRADL
Definition remux.c:1680
@ nutSliceSegmentTSAR
Definition remux.c:1672
@ nutSliceSegmentRADLR
Definition remux.c:1676
@ nutSliceSegmentTrailingR
Definition remux.c:1670
@ nutVideoParameterSet
Definition remux.c:1685
@ nutSequenceParameterSet
Definition remux.c:1686
@ nutSliceSegmentTSAN
Definition remux.c:1671
@ nutSliceSegmentRASLN
Definition remux.c:1677
@ nutSuffixSEI
Definition remux.c:1693
@ nutSliceSegmentBLAWLP
Definition remux.c:1679
@ nutEndOfBitstream
Definition remux.c:1690
@ nutSliceSegmentBLANLP
Definition remux.c:1681
@ nutSliceSegmentRASLR
Definition remux.c:1678
@ nutPictureParameterSet
Definition remux.c:1687
@ nutNonVCLRes3
Definition remux.c:1695
@ nutSliceSegmentCRANUT
Definition remux.c:1684
@ nutSliceSegmentTrailingN
Definition remux.c:1669
@ nutNonVCLRes0
Definition remux.c:1694
@ nutFillerData
Definition remux.c:1691
@ nutSliceSegmentSTSAR
Definition remux.c:1674
@ nutEndOfSequence
Definition remux.c:1689
void ParseSequenceParameterSet(void)
Definition remux.c:1744
bool seenIndependentFrame
Definition remux.c:1249
cMpeg2Parser(void)
Definition remux.c:1268
bool seenScanType
Definition remux.c:1251
int lastIFrameTemporalReference
Definition remux.c:1250
uint32_t scanner
Definition remux.c:1248
const double frame_rate_table[9]
Definition remux.c:1252
virtual int Parse(const uchar *Data, int Length, int Pid) override
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition remux.c:1276
int MakeCRC(uchar *Target, const uchar *Data, int Length)
Definition remux.c:455
uchar * GetPmt(int &Index)
Returns a pointer to the Index'th TS packet of the PMT section.
Definition remux.c:604
void SetChannel(const cChannel *Channel)
Sets the Channel for which the PAT/PMT shall be generated.
Definition remux.c:589
void IncEsInfoLength(int Length)
Definition remux.c:388
void IncCounter(int &Counter, uchar *TsPacket)
Definition remux.c:375
cPatPmtGenerator(const cChannel *Channel=NULL)
Definition remux.c:365
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:583
int numPmtPackets
Definition remux.h:302
uchar * esInfoLength
Definition remux.h:308
uchar pat[TS_SIZE]
Definition remux.h:300
int MakeAC3Descriptor(uchar *Target, uchar Type)
Definition remux.c:409
void GeneratePat(void)
Generates a PAT section for later use with GetPat().
Definition remux.c:485
uchar * GetPat(void)
Returns a pointer to the PAT section, which consists of exactly one TS packet.
Definition remux.c:598
uchar pmt[MAX_PMT_TS][TS_SIZE]
Definition remux.h:301
int MakeLanguageDescriptor(uchar *Target, const char *Language)
Definition remux.c:436
void GeneratePmt(const cChannel *Channel)
Generates a PMT section for the given Channel, for later use with GetPmt().
Definition remux.c:514
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:470
int MakeStream(uchar *Target, uchar Type, int Pid)
Definition remux.c:397
int MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
Definition remux.c:419
void IncVersion(int &Version)
Definition remux.c:382
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:942
int dpids[MAXDPIDS+1]
Definition remux.h:366
uchar pmt[MAX_SECTION_SIZE]
Definition remux.h:355
int apids[MAXAPIDS+1]
Definition remux.h:363
cPatPmtParser(bool UpdatePrimaryDevice=false)
Definition remux.c:615
void Reset(void)
Resets the parser.
Definition remux.c:621
void ParsePat(const uchar *Data, int Length)
Parses the PAT data from the single TS packet in Data.
Definition remux.c:631
int pmtSize
Definition remux.h:356
char dlangs[MAXDPIDS][MAXLANGCODE2]
Definition remux.h:368
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:923
int patVersion
Definition remux.h:357
int pmtPids[MAX_PMT_PIDS+1]
Definition remux.h:359
uchar subtitlingTypes[MAXSPIDS]
Definition remux.h:371
int dtypes[MAXDPIDS+1]
Definition remux.h:367
uint16_t ancillaryPageIds[MAXSPIDS]
Definition remux.h:373
int SectionLength(const uchar *Data, int Length)
Definition remux.h:377
void ParsePmt(const uchar *Data, int Length)
Parses the PMT data from the single TS packet in Data.
Definition remux.c:663
bool completed
Definition remux.h:375
bool IsPmtPid(int Pid) const
Returns true if Pid the one of the PMT pids as defined by the current PAT.
Definition remux.h:400
uint16_t compositionPageIds[MAXSPIDS]
Definition remux.h:372
char alangs[MAXAPIDS][MAXLANGCODE2]
Definition remux.h:365
int spids[MAXSPIDS+1]
Definition remux.h:369
int pmtVersion
Definition remux.h:358
bool updatePrimaryDevice
Definition remux.h:374
char slangs[MAXSPIDS][MAXLANGCODE2]
Definition remux.h:370
int atypes[MAXAPIDS+1]
Definition remux.h:364
bool NewMissing(void)
Definition remux.c:2115
void SetFrameDelta(int FrameDelta)
Definition remux.c:2039
bool iFrameNoPts
Definition remux.c:2032
int frameDelta
Definition remux.c:2033
void Clear(void)
Definition remux.c:2052
void Process(void)
Definition remux.c:2060
int totalMissing
Definition remux.c:2034
int64_t lastPts
Definition remux.c:2031
int oldMissing
Definition remux.c:2035
int Missing(void)
Definition remux.c:2122
cPtsChecker(void)
Definition remux.c:2046
cVector< int64_t > pts
Definition remux.c:2030
void AddPts(int64_t Pts, bool IndependentFrame=false)
Definition remux.c:2102
static void SetBrokenLink(uchar *Data, int Length)
Definition remux.c:102
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition tools.c:1216
uchar counter[MAXPID]
Definition remux.c:1953
cTsChecker(void)
Definition remux.c:1966
void CheckTs(const uchar *Data, int Length)
Definition remux.c:1997
int errors
Definition remux.c:1954
int Errors(void)
Definition remux.c:1961
void Reset(void)
Definition remux.c:1972
void Report(int Pid, const char *Message)
Definition remux.c:1983
void Clear(void)
Definition remux.c:1977
int oldErrors
Definition remux.c:1955
bool NewErrors(void)
Definition remux.c:1990
int numPacketsPid
Definition remux.h:232
int pid
Definition remux.h:230
cTsPayload(void)
Definition remux.c:248
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:252
int Used(void)
Returns the number of raw bytes that have already been used (e.g.
Definition remux.h:258
bool Eof(void) const
Returns true if all available bytes of the TS payload have been processed.
Definition remux.h:262
void SetByte(uchar Byte, int Index)
Sets the TS data byte at the given Index to the value Byte.
Definition remux.c:332
uchar GetByte(void)
Gets the next byte of the TS payload, skipping any intermediate TS header data.
Definition remux.c:282
bool AtTsStart(void)
Returns true if this payload handler is currently pointing to first byte of a TS packet.
Definition remux.h:249
int GetLastIndex(void)
Returns the index into the TS data of the payload byte that has most recently been read.
Definition remux.c:327
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:274
bool SkipPesHeader(void)
Skips all bytes belonging to the PES header of the payload.
Definition remux.c:322
int index
Definition remux.h:231
int numPacketsOther
Definition remux.h:233
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:355
uchar SetEof(void)
Definition remux.c:261
int length
Definition remux.h:229
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:338
void Reset(void)
Definition remux.c:267
uchar * data
Definition remux.h:228
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:315
int lastLength
Definition remux.h:457
bool repeatLast
Definition remux.h:458
uchar * lastData
Definition remux.h:456
uchar * data
Definition remux.h:452
void PutTs(const uchar *Data, int Length)
Puts the payload data of the single TS packet at Data into the converter.
Definition remux.c:1050
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:1127
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:1079
cTsToPes(void)
Definition remux.c:1038
int length
Definition remux.h:454
~cTsToPes()
Definition remux.c:1045
void Reset(void)
Resets the converter.
Definition remux.c:1132
int offset
Definition remux.h:455
int size
Definition remux.h:453
cSetup Setup
Definition config.c:372
@ ttSubtitle
Definition device.h:70
@ ttDolby
Definition device.h:67
@ ttAudio
Definition device.h:64
const char * I18nNormalizeLanguageCode(const char *Code)
Returns a 3 letter language code that may not be zero terminated.
Definition i18n.c:300
@ EnhancedAC3DescriptorTag
Definition si.h:136
@ SubtitlingDescriptorTag
Definition si.h:102
@ ISO639LanguageDescriptorTag
Definition si.h:60
@ ParentalRatingDescriptorTag
Definition si.h:98
@ AC3DescriptorTag
Definition si.h:119
#define DEFAULTFRAMESPERSECOND
Definition recording.h:401
void TsSetPcr(uchar *p, int64_t Pcr)
Definition remux.c:131
const char * AspectRatioTexts[]
Definition remux.c:2165
#define WRN_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION
Definition remux.c:27
#define dbgframes(a...)
Definition remux.c:24
#define WRN_TS_PACKETS_FOR_FRAME_DETECTOR
Definition remux.c:28
#define SETPID(p)
void PesDump(const char *Name, const u_char *Data, int Length)
Definition remux.c:1168
static bool DebugFrames
Definition remux.c:21
int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
Returns the difference between two PTS values.
Definition remux.c:236
const char * ScanTypeChars
Definition remux.c:2164
#define MAXPESLENGTH
Definition remux.c:1077
#define P_PMT_PID
Definition remux.c:467
void TsHidePayload(uchar *p)
Definition remux.c:121
static int CmpUint32(const void *p1, const void *p2)
Definition remux.c:2202
#define MAXPID
Definition remux.c:468
void PesSetDts(uchar *p, int64_t Dts)
Definition remux.c:227
#define EMPTY_SCANNER
Definition remux.c:30
static bool DebugPatPmt
Definition remux.c:20
static bool DebugChecks
Definition remux.c:1938
int64_t TsGetDts(const uchar *p, int l)
Definition remux.c:175
#define TS_CC_UNKNOWN
Definition remux.c:1949
static int ComparePts(const void *a, const void *b)
Definition remux.c:2024
void TsSetDts(uchar *p, int l, int64_t Dts)
Definition remux.c:202
void TsSetPts(uchar *p, int l, int64_t Pts)
Definition remux.c:188
ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader)
Definition remux.c:32
#define dbgpatpmt(a...)
Definition remux.c:23
#define VIDEO_STREAM_S
Definition remux.c:98
void PesSetPts(uchar *p, int64_t Pts)
Definition remux.c:218
void BlockDump(const char *Name, const u_char *Data, int Length)
Definition remux.c:1142
int TsSync(const uchar *Data, int Length, const char *File, const char *Function, int Line)
Definition remux.c:147
#define SETPIDS(l)
#define P_TSID
Definition remux.c:466
void TsDump(const char *Name, const u_char *Data, int Length)
Definition remux.c:1153
int64_t TsGetPts(const uchar *p, int l, int Pid)
Definition remux.c:160
#define MAX_TS_PACKETS_FOR_VIDEO_FRAME_DETECTION
Definition remux.c:26
bool TsError(const uchar *p)
Definition remux.h:77
#define TS_ADAPT_PCR
Definition remux.h:46
int TsPid(const uchar *p)
Definition remux.h:82
bool TsHasPayload(const uchar *p)
Definition remux.h:62
#define MAX33BIT
Definition remux.h:59
#define MAX_PMT_PIDS
Definition remux.h:351
int PesPayloadOffset(const uchar *p)
Definition remux.h:178
#define PATPID
Definition remux.h:52
bool TsIsScrambled(const uchar *p)
Definition remux.h:93
uchar TsContinuityCounter(const uchar *p)
Definition remux.h:98
int TsGetPayload(const uchar **p)
Definition remux.h:114
#define TS_PAYLOAD_EXISTS
Definition remux.h:41
bool PesHasPts(const uchar *p)
Definition remux.h:183
bool PesLongEnough(int Length)
Definition remux.h:163
#define TS_SIZE
Definition remux.h:34
eAspectRatio
Definition remux.h:514
@ ar_16_9
Definition remux.h:518
@ ar_1_1
Definition remux.h:516
@ arUnknown
Definition remux.h:515
@ ar_4_3
Definition remux.h:517
@ ar_2_21_1
Definition remux.h:519
eScanType
Definition remux.h:507
@ stInterlaced
Definition remux.h:510
@ stProgressive
Definition remux.h:509
@ stUnknown
Definition remux.h:508
#define MAX_SECTION_SIZE
Definition remux.h:295
int64_t PesGetDts(const uchar *p)
Definition remux.h:202
#define TS_ADAPT_FIELD_EXISTS
Definition remux.h:40
int64_t PesGetPts(const uchar *p)
Definition remux.h:193
bool TsPayloadStart(const uchar *p)
Definition remux.h:72
#define TS_SYNC(Data, Length)
Definition remux.h:149
int TsPayloadOffset(const uchar *p)
Definition remux.h:108
#define MAXPID
Definition remux.h:55
bool PesHasDts(const uchar *p)
Definition remux.h:188
#define PCRFACTOR
Definition remux.h:58
#define TS_SYNC_BYTE
Definition remux.h:33
int64_t TsGetPts(const uchar *p, int l, int Pid=-1)
Definition remux.c:160
bool PesHasLength(const uchar *p)
Definition remux.h:168
bool TsHasAdaptationField(const uchar *p)
Definition remux.h:67
#define PTSTICKS
Definition remux.h:57
ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader=NULL)
Definition remux.c:32
#define MIN_TS_PACKETS_FOR_FRAME_DETECTOR
Definition remux.h:503
int PesLength(const uchar *p)
Definition remux.h:173
ePesHeader
Definition remux.h:16
@ phMPEG2
Definition remux.h:20
@ phNeedMoreData
Definition remux.h:17
@ phInvalid
Definition remux.h:18
@ phMPEG1
Definition remux.h:19
#define EITPID
Definition remux.h:54
#define TS_CONT_CNT_MASK
Definition remux.h:42
#define TS_PAYLOAD_START
Definition remux.h:36
cString TimeToString(time_t t)
Converts the given time to a string of the form "www mmm dd hh:mm:ss yyyy".
Definition tools.c:1292
char * strn0cpy(char *dest, const char *src, size_t n)
Definition tools.c:135
unsigned char uchar
Definition tools.h:31
#define dsyslog(a...)
Definition tools.h:37
T min(T a, T b)
Definition tools.h:63
T max(T a, T b)
Definition tools.h:64
#define esyslog(a...)
Definition tools.h:35
#define KILOBYTE(n)
Definition tools.h:44