vdr 2.6.6
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.7 2024/01/23 19:33:45 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)
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
173int64_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
186void 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
200void 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
216void 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
225void 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
234int64_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
254cTsPayload::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
272void 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();
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
301 }
302 else
303 return SetEof();
304 }
305 }
306 return data[index++];
307 }
308 return 0x00;
309}
310
312{
313 while (Bytes-- > 0)
314 GetByte();
315 return !Eof();
316}
317
322
324{
325 return index - 1;
326}
327
328void cTsPayload::SetByte(uchar Byte, int Index)
329{
330 if (Index >= 0 && Index < length)
331 data[Index] = Byte;
332}
333
334bool 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
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// --- cPatPmtGenerator ------------------------------------------------------
360
362{
363 numPmtPackets = 0;
366 pmtPid = 0;
367 esInfoLength = NULL;
368 SetChannel(Channel);
369}
370
371void cPatPmtGenerator::IncCounter(int &Counter, uchar *TsPacket)
372{
373 TsPacket[3] = (TsPacket[3] & 0xF0) | Counter;
374 if (++Counter > 0x0F)
375 Counter = 0x00;
376}
377
379{
380 if (++Version > 0x1F)
381 Version = 0x00;
382}
383
385{
386 if (esInfoLength) {
387 Length += ((*esInfoLength & 0x0F) << 8) | *(esInfoLength + 1);
388 *esInfoLength = 0xF0 | (Length >> 8);
389 *(esInfoLength + 1) = Length;
390 }
391}
392
393int cPatPmtGenerator::MakeStream(uchar *Target, uchar Type, int Pid)
394{
395 int i = 0;
396 Target[i++] = Type; // stream type
397 Target[i++] = 0xE0 | (Pid >> 8); // dummy (3), pid hi (5)
398 Target[i++] = Pid; // pid lo
399 esInfoLength = &Target[i];
400 Target[i++] = 0xF0; // dummy (4), ES info length hi
401 Target[i++] = 0x00; // ES info length lo
402 return i;
403}
404
406{
407 int i = 0;
408 Target[i++] = Type;
409 Target[i++] = 0x01; // length
410 Target[i++] = 0x00;
412 return i;
413}
414
415int cPatPmtGenerator::MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
416{
417 int i = 0;
418 Target[i++] = SI::SubtitlingDescriptorTag;
419 Target[i++] = 0x08; // length
420 Target[i++] = *Language++;
421 Target[i++] = *Language++;
422 Target[i++] = *Language++;
423 Target[i++] = SubtitlingType;
424 Target[i++] = CompositionPageId >> 8;
425 Target[i++] = CompositionPageId & 0xFF;
426 Target[i++] = AncillaryPageId >> 8;
427 Target[i++] = AncillaryPageId & 0xFF;
429 return i;
430}
431
432int cPatPmtGenerator::MakeLanguageDescriptor(uchar *Target, const char *Language)
433{
434 int i = 0;
436 int Length = i++;
437 Target[Length] = 0x00; // length
438 for (const char *End = Language + strlen(Language); Language < End; ) {
439 Target[i++] = *Language++;
440 Target[i++] = *Language++;
441 Target[i++] = *Language++;
442 Target[i++] = 0x00; // audio type
443 Target[Length] += 0x04; // length
444 if (*Language == '+')
445 Language++;
446 }
448 return i;
449}
450
451int cPatPmtGenerator::MakeCRC(uchar *Target, const uchar *Data, int Length)
452{
453 int crc = SI::CRC32::crc32((const char *)Data, Length, 0xFFFFFFFF);
454 int i = 0;
455 Target[i++] = crc >> 24;
456 Target[i++] = crc >> 16;
457 Target[i++] = crc >> 8;
458 Target[i++] = crc;
459 return i;
460}
461
462#define P_TSID 0x8008 // pseudo TS ID
463#define P_PMT_PID 0x0084 // pseudo PMT pid
464#define MAXPID 0x2000 // the maximum possible number of pids
465
467{
468 bool Used[MAXPID] = { false };
469#define SETPID(p) { if ((p) >= 0 && (p) < MAXPID) Used[p] = true; }
470#define SETPIDS(l) { const int *p = l; while (*p) { SETPID(*p); p++; } }
471 SETPID(Channel->Vpid());
472 SETPID(Channel->Ppid());
473 SETPID(Channel->Tpid());
474 SETPIDS(Channel->Apids());
475 SETPIDS(Channel->Dpids());
476 SETPIDS(Channel->Spids());
477 for (pmtPid = P_PMT_PID; Used[pmtPid]; pmtPid++)
478 ;
479}
480
482{
483 memset(pat, 0xFF, sizeof(pat));
484 uchar *p = pat;
485 int i = 0;
486 p[i++] = TS_SYNC_BYTE; // TS indicator
487 p[i++] = TS_PAYLOAD_START | (PATPID >> 8); // flags (3), pid hi (5)
488 p[i++] = PATPID & 0xFF; // pid lo
489 p[i++] = 0x10; // flags (4), continuity counter (4)
490 p[i++] = 0x00; // pointer field (payload unit start indicator is set)
491 int PayloadStart = i;
492 p[i++] = 0x00; // table id
493 p[i++] = 0xB0; // section syntax indicator (1), dummy (3), section length hi (4)
494 int SectionLength = i;
495 p[i++] = 0x00; // section length lo (filled in later)
496 p[i++] = P_TSID >> 8; // TS id hi
497 p[i++] = P_TSID & 0xFF; // TS id lo
498 p[i++] = 0xC1 | (patVersion << 1); // dummy (2), version number (5), current/next indicator (1)
499 p[i++] = 0x00; // section number
500 p[i++] = 0x00; // last section number
501 p[i++] = pmtPid >> 8; // program number hi
502 p[i++] = pmtPid & 0xFF; // program number lo
503 p[i++] = 0xE0 | (pmtPid >> 8); // dummy (3), PMT pid hi (5)
504 p[i++] = pmtPid & 0xFF; // PMT pid lo
505 pat[SectionLength] = i - SectionLength - 1 + 4; // -1 = SectionLength storage, +4 = length of CRC
506 MakeCRC(pat + i, pat + PayloadStart, i - PayloadStart);
508}
509
511{
512 // generate the complete PMT section:
514 memset(buf, 0xFF, sizeof(buf));
515 numPmtPackets = 0;
516 if (Channel) {
517 int Vpid = Channel->Vpid();
518 int Ppid = Channel->Ppid();
519 uchar *p = buf;
520 int i = 0;
521 p[i++] = 0x02; // table id
522 int SectionLength = i;
523 p[i++] = 0xB0; // section syntax indicator (1), dummy (3), section length hi (4)
524 p[i++] = 0x00; // section length lo (filled in later)
525 p[i++] = pmtPid >> 8; // program number hi
526 p[i++] = pmtPid & 0xFF; // program number lo
527 p[i++] = 0xC1 | (pmtVersion << 1); // dummy (2), version number (5), current/next indicator (1)
528 p[i++] = 0x00; // section number
529 p[i++] = 0x00; // last section number
530 p[i++] = 0xE0 | (Ppid >> 8); // dummy (3), PCR pid hi (5)
531 p[i++] = Ppid; // PCR pid lo
532 p[i++] = 0xF0; // dummy (4), program info length hi (4)
533 p[i++] = 0x00; // program info length lo
534
535 if (Vpid)
536 i += MakeStream(buf + i, Channel->Vtype(), Vpid);
537 for (int n = 0; Channel->Apid(n); n++) {
538 i += MakeStream(buf + i, Channel->Atype(n), Channel->Apid(n));
539 const char *Alang = Channel->Alang(n);
540 i += MakeLanguageDescriptor(buf + i, Alang);
541 }
542 for (int n = 0; Channel->Dpid(n); n++) {
543 i += MakeStream(buf + i, 0x06, Channel->Dpid(n));
544 i += MakeAC3Descriptor(buf + i, Channel->Dtype(n));
545 i += MakeLanguageDescriptor(buf + i, Channel->Dlang(n));
546 }
547 for (int n = 0; Channel->Spid(n); n++) {
548 i += MakeStream(buf + i, 0x06, Channel->Spid(n));
549 i += MakeSubtitlingDescriptor(buf + i, Channel->Slang(n), Channel->SubtitlingType(n), Channel->CompositionPageId(n), Channel->AncillaryPageId(n));
550 }
551
552 int sl = i - SectionLength - 2 + 4; // -2 = SectionLength storage, +4 = length of CRC
553 buf[SectionLength] |= (sl >> 8) & 0x0F;
554 buf[SectionLength + 1] = sl;
555 MakeCRC(buf + i, buf, i);
556 // split the PMT section into several TS packets:
557 uchar *q = buf;
558 bool pusi = true;
559 while (i > 0) {
560 uchar *p = pmt[numPmtPackets++];
561 int j = 0;
562 p[j++] = TS_SYNC_BYTE; // TS indicator
563 p[j++] = (pusi ? TS_PAYLOAD_START : 0x00) | (pmtPid >> 8); // flags (3), pid hi (5)
564 p[j++] = pmtPid & 0xFF; // pid lo
565 p[j++] = 0x10; // flags (4), continuity counter (4)
566 if (pusi) {
567 p[j++] = 0x00; // pointer field (payload unit start indicator is set)
568 pusi = false;
569 }
570 int l = TS_SIZE - j;
571 memcpy(p + j, q, l);
572 q += l;
573 i -= l;
574 }
576 }
577}
578
579void cPatPmtGenerator::SetVersions(int PatVersion, int PmtVersion)
580{
581 patVersion = PatVersion & 0x1F;
582 pmtVersion = PmtVersion & 0x1F;
583}
584
586{
587 if (Channel) {
588 GeneratePmtPid(Channel);
589 GeneratePat();
590 GeneratePmt(Channel);
591 }
592}
593
595{
597 return pat;
598}
599
601{
602 if (Index < numPmtPackets) {
603 IncCounter(pmtCounter, pmt[Index]);
604 return pmt[Index++];
605 }
606 return NULL;
607}
608
609// --- cPatPmtParser ---------------------------------------------------------
610
611cPatPmtParser::cPatPmtParser(bool UpdatePrimaryDevice)
612{
613 updatePrimaryDevice = UpdatePrimaryDevice;
614 Reset();
615}
616
618{
619 completed = false;
620 pmtSize = 0;
621 patVersion = pmtVersion = -1;
622 pmtPids[0] = 0;
623 vpid = vtype = 0;
624 ppid = 0;
625}
626
627void cPatPmtParser::ParsePat(const uchar *Data, int Length)
628{
629 // Unpack the TS packet:
630 int PayloadOffset = TsPayloadOffset(Data);
631 Data += PayloadOffset;
632 Length -= PayloadOffset;
633 // The PAT is always assumed to fit into a single TS packet
634 if ((Length -= Data[0] + 1) <= 0)
635 return;
636 Data += Data[0] + 1; // process pointer_field
637 SI::PAT Pat(Data, false);
638 if (Pat.CheckCRCAndParse()) {
639 dbgpatpmt("PAT: TSid = %d, c/n = %d, v = %d, s = %d, ls = %d\n", Pat.getTransportStreamId(), Pat.getCurrentNextIndicator(), Pat.getVersionNumber(), Pat.getSectionNumber(), Pat.getLastSectionNumber());
640 if (patVersion == Pat.getVersionNumber())
641 return;
642 int NumPmtPids = 0;
644 for (SI::Loop::Iterator it; Pat.associationLoop.getNext(assoc, it); ) {
645 dbgpatpmt(" isNITPid = %d\n", assoc.isNITPid());
646 if (!assoc.isNITPid()) {
647 if (NumPmtPids <= MAX_PMT_PIDS)
648 pmtPids[NumPmtPids++] = assoc.getPid();
649 dbgpatpmt(" service id = %d, pid = %d\n", assoc.getServiceId(), assoc.getPid());
650 }
651 }
652 pmtPids[NumPmtPids] = 0;
654 }
655 else
656 esyslog("ERROR: can't parse PAT");
657}
658
659void cPatPmtParser::ParsePmt(const uchar *Data, int Length)
660{
661 // Unpack the TS packet:
662 bool PayloadStart = TsPayloadStart(Data);
663 int PayloadOffset = TsPayloadOffset(Data);
664 Data += PayloadOffset;
665 Length -= PayloadOffset;
666 // The PMT may extend over several TS packets, so we need to assemble them
667 if (PayloadStart) {
668 pmtSize = 0;
669 if ((Length -= Data[0] + 1) <= 0)
670 return;
671 Data += Data[0] + 1; // this is the first packet
672 if (SectionLength(Data, Length) > Length) {
673 if (Length <= int(sizeof(pmt))) {
674 memcpy(pmt, Data, Length);
675 pmtSize = Length;
676 }
677 else
678 esyslog("ERROR: PMT packet length too big (%d byte)!", Length);
679 return;
680 }
681 // the packet contains the entire PMT section, so we run into the actual parsing
682 }
683 else if (pmtSize > 0) {
684 // this is a following packet, so we add it to the pmt storage
685 if (Length <= int(sizeof(pmt)) - pmtSize) {
686 memcpy(pmt + pmtSize, Data, Length);
687 pmtSize += Length;
688 }
689 else {
690 esyslog("ERROR: PMT section length too big (%d byte)!", pmtSize + Length);
691 pmtSize = 0;
692 }
694 return; // more packets to come
695 // the PMT section is now complete, so we run into the actual parsing
696 Data = pmt;
697 }
698 else
699 return; // fragment of broken packet - ignore
700 SI::PMT Pmt(Data, false);
701 if (Pmt.CheckCRCAndParse()) {
702 dbgpatpmt("PMT: sid = %d, c/n = %d, v = %d, s = %d, ls = %d\n", Pmt.getServiceId(), Pmt.getCurrentNextIndicator(), Pmt.getVersionNumber(), Pmt.getSectionNumber(), Pmt.getLastSectionNumber());
703 dbgpatpmt(" pcr = %d\n", Pmt.getPCRPid());
704 if (pmtVersion == Pmt.getVersionNumber())
705 return;
708 int NumApids = 0;
709 int NumDpids = 0;
710 int NumSpids = 0;
711 vpid = vtype = 0;
712 ppid = 0;
713 apids[0] = 0;
714 dpids[0] = 0;
715 spids[0] = 0;
716 atypes[0] = 0;
717 dtypes[0] = 0;
718 SI::PMT::Stream stream;
719 for (SI::Loop::Iterator it; Pmt.streamLoop.getNext(stream, it); ) {
720 dbgpatpmt(" stream type = %02X, pid = %d", stream.getStreamType(), stream.getPid());
721 switch (stream.getStreamType()) {
722 case 0x01: // STREAMTYPE_11172_VIDEO
723 case 0x02: // STREAMTYPE_13818_VIDEO
724 case 0x1B: // H.264
725 case 0x24: // H.265
726 vpid = stream.getPid();
727 vtype = stream.getStreamType();
728 ppid = Pmt.getPCRPid();
729 break;
730 case 0x03: // STREAMTYPE_11172_AUDIO
731 case 0x04: // STREAMTYPE_13818_AUDIO
732 case 0x0F: // ISO/IEC 13818-7 Audio with ADTS transport syntax
733 case 0x11: // ISO/IEC 14496-3 Audio with LATM transport syntax
734 {
735 if (NumApids < MAXAPIDS) {
736 apids[NumApids] = stream.getPid();
737 atypes[NumApids] = stream.getStreamType();
738 *alangs[NumApids] = 0;
740 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
741 switch (d->getDescriptorTag()) {
745 char *s = alangs[NumApids];
746 int n = 0;
747 for (SI::Loop::Iterator it; ld->languageLoop.getNext(l, it); ) {
748 if (*ld->languageCode != '-') { // some use "---" to indicate "none"
749 dbgpatpmt(" '%s'", l.languageCode);
750 if (n > 0)
751 *s++ = '+';
753 s += strlen(s);
754 if (n++ > 1)
755 break;
756 }
757 }
758 }
759 break;
760 default: ;
761 }
762 delete d;
763 }
765 cDevice::PrimaryDevice()->SetAvailableTrack(ttAudio, NumApids, apids[NumApids], alangs[NumApids]);
766 NumApids++;
767 apids[NumApids] = 0;
768 }
769 }
770 break;
771 case 0x06: // STREAMTYPE_13818_PES_PRIVATE
772 {
773 int dpid = 0;
774 int dtype = 0;
775 char lang[MAXLANGCODE1] = "";
777 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
778 switch (d->getDescriptorTag()) {
781 dbgpatpmt(" AC3");
782 dpid = stream.getPid();
783 dtype = d->getDescriptorTag();
784 break;
786 dbgpatpmt(" subtitling");
787 if (NumSpids < MAXSPIDS) {
788 spids[NumSpids] = stream.getPid();
789 *slangs[NumSpids] = 0;
790 subtitlingTypes[NumSpids] = 0;
791 compositionPageIds[NumSpids] = 0;
792 ancillaryPageIds[NumSpids] = 0;
795 char *s = slangs[NumSpids];
796 int n = 0;
797 for (SI::Loop::Iterator it; sd->subtitlingLoop.getNext(sub, it); ) {
798 if (sub.languageCode[0]) {
799 dbgpatpmt(" '%s'", sub.languageCode);
800 subtitlingTypes[NumSpids] = sub.getSubtitlingType();
802 ancillaryPageIds[NumSpids] = sub.getAncillaryPageId();
803 if (n > 0)
804 *s++ = '+';
806 s += strlen(s);
807 if (n++ > 1)
808 break;
809 }
810 }
812 cDevice::PrimaryDevice()->SetAvailableTrack(ttSubtitle, NumSpids, spids[NumSpids], slangs[NumSpids]);
813 NumSpids++;
814 spids[NumSpids] = 0;
815 }
816 break;
819 dbgpatpmt(" '%s'", ld->languageCode);
821 }
822 break;
823 default: ;
824 }
825 delete d;
826 }
827 if (dpid) {
828 if (NumDpids < MAXDPIDS) {
829 dpids[NumDpids] = dpid;
830 dtypes[NumDpids] = dtype;
831 strn0cpy(dlangs[NumDpids], lang, sizeof(dlangs[NumDpids]));
833 cDevice::PrimaryDevice()->SetAvailableTrack(ttDolby, NumDpids, dpid, lang);
834 NumDpids++;
835 dpids[NumDpids] = 0;
836 }
837 }
838 }
839 break;
840 case 0x81: // STREAMTYPE_USER_PRIVATE - AC3 audio for ATSC and BD
841 case 0x82: // STREAMTYPE_USER_PRIVATE - DTS audio for BD
842 case 0x87: // eac3
843 {
844 dbgpatpmt(" %s",
845 stream.getStreamType() == 0x81 ? "AC3" :
846 stream.getStreamType() == 0x87 ? "AC3" :
847 stream.getStreamType() == 0x82 ? "DTS" : "");
848 char lang[MAXLANGCODE1] = { 0 };
850 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
851 switch (d->getDescriptorTag()) {
854 dbgpatpmt(" '%s'", ld->languageCode);
856 }
857 break;
858 default: ;
859 }
860 delete d;
861 }
862 if (NumDpids < MAXDPIDS) {
863 dpids[NumDpids] = stream.getPid();
864 dtypes[NumDpids] = SI::AC3DescriptorTag;
865 strn0cpy(dlangs[NumDpids], lang, sizeof(dlangs[NumDpids]));
867 cDevice::PrimaryDevice()->SetAvailableTrack(ttDolby, NumDpids, stream.getPid(), lang);
868 NumDpids++;
869 dpids[NumDpids] = 0;
870 }
871 }
872 break;
873 case 0x90: // PGS subtitles for BD
874 {
875 dbgpatpmt(" subtitling");
876 char lang[MAXLANGCODE1] = { 0 };
878 for (SI::Loop::Iterator it; (d = stream.streamDescriptors.getNext(it)); ) {
879 switch (d->getDescriptorTag()) {
882 dbgpatpmt(" '%s'", ld->languageCode);
884 if (NumSpids < MAXSPIDS) {
885 spids[NumSpids] = stream.getPid();
886 *slangs[NumSpids] = 0;
887 subtitlingTypes[NumSpids] = 0;
888 compositionPageIds[NumSpids] = 0;
889 ancillaryPageIds[NumSpids] = 0;
891 cDevice::PrimaryDevice()->SetAvailableTrack(ttSubtitle, NumSpids, stream.getPid(), lang);
892 NumSpids++;
893 spids[NumSpids] = 0;
894 }
895 }
896 break;
897 default: ;
898 }
899 delete d;
900 }
901 }
902 break;
903 default: ;
904 }
905 dbgpatpmt("\n");
909 }
910 }
912 completed = true;
913 }
914 else
915 esyslog("ERROR: can't parse PMT");
916 pmtSize = 0;
917}
918
919bool cPatPmtParser::ParsePatPmt(const uchar *Data, int Length)
920{
921 while (Length >= TS_SIZE) {
922 if (*Data != TS_SYNC_BYTE)
923 break; // just for safety
924 int Pid = TsPid(Data);
925 if (Pid == PATPID)
926 ParsePat(Data, TS_SIZE);
927 else if (IsPmtPid(Pid)) {
928 ParsePmt(Data, TS_SIZE);
929 if (patVersion >= 0 && pmtVersion >= 0)
930 return true;
931 }
932 Data += TS_SIZE;
933 Length -= TS_SIZE;
934 }
935 return false;
936}
937
938bool cPatPmtParser::GetVersions(int &PatVersion, int &PmtVersion) const
939{
940 PatVersion = patVersion;
941 PmtVersion = pmtVersion;
942 return patVersion >= 0 && pmtVersion >= 0;
943}
944
945// --- cEitGenerator ---------------------------------------------------------
946
948{
949 counter = 0;
950 version = 0;
951 if (Sid)
952 Generate(Sid);
953}
954
955uint16_t cEitGenerator::YMDtoMJD(int Y, int M, int D)
956{
957 int L = (M < 3) ? 1 : 0;
958 return 14956 + D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
959}
960
962{
964 *p++ = 0x04; // descriptor length
965 *p++ = '9'; // country code "902" ("All countries") -> EN 300 468 / 6.2.28; www.dvbservices.com/country_codes/index.php
966 *p++ = '0';
967 *p++ = '2';
968 *p++ = ParentalRating;
969 return p;
970}
971
973{
974 uchar *PayloadStart;
975 uchar *SectionStart;
976 uchar *DescriptorsStart;
977 memset(eit, 0xFF, sizeof(eit));
978 struct tm tm_r;
979 time_t t = time(NULL) - 3600; // let's have the event start one hour in the past
980 tm *tm = localtime_r(&t, &tm_r);
981 uint16_t MJD = YMDtoMJD(tm->tm_year, tm->tm_mon + 1, tm->tm_mday);
982 uchar *p = eit;
983 // TS header:
984 *p++ = TS_SYNC_BYTE;
985 *p++ = TS_PAYLOAD_START;
986 *p++ = EITPID;
987 *p++ = 0x10 | (counter++ & 0x0F); // continuity counter
988 *p++ = 0x00; // pointer field (payload unit start indicator is set)
989 // payload:
990 PayloadStart = p;
991 *p++ = 0x4E; // TID present/following event on this transponder
992 *p++ = 0xF0;
993 *p++ = 0x00; // section length
994 SectionStart = p;
995 *p++ = Sid >> 8;
996 *p++ = Sid & 0xFF;
997 *p++ = 0xC1 | (version << 1);
998 *p++ = 0x00; // section number
999 *p++ = 0x00; // last section number
1000 *p++ = 0x00; // transport stream id
1001 *p++ = 0x00; // ...
1002 *p++ = 0x00; // original network id
1003 *p++ = 0x00; // ...
1004 *p++ = 0x00; // segment last section number
1005 *p++ = 0x4E; // last table id
1006 *p++ = 0x00; // event id
1007 *p++ = 0x01; // ...
1008 *p++ = MJD >> 8; // start time
1009 *p++ = MJD & 0xFF; // ...
1010 *p++ = tm->tm_hour; // ...
1011 *p++ = tm->tm_min; // ...
1012 *p++ = tm->tm_sec; // ...
1013 *p++ = 0x24; // duration (one day, should cover everything)
1014 *p++ = 0x00; // ...
1015 *p++ = 0x00; // ...
1016 *p++ = 0x90; // running status, free/CA mode
1017 *p++ = 0x00; // descriptors loop length
1018 DescriptorsStart = p;
1020 // fill in lengths:
1021 *(SectionStart - 1) = p - SectionStart + 4; // +4 = length of CRC
1022 *(DescriptorsStart - 1) = p - DescriptorsStart;
1023 // checksum
1024 int crc = SI::CRC32::crc32((char *)PayloadStart, p - PayloadStart, 0xFFFFFFFF);
1025 *p++ = crc >> 24;
1026 *p++ = crc >> 16;
1027 *p++ = crc >> 8;
1028 *p++ = crc;
1029 return eit;
1030}
1031
1032// --- cTsToPes --------------------------------------------------------------
1033
1035{
1036 data = NULL;
1037 size = 0;
1038 Reset();
1039}
1040
1042{
1043 free(data);
1044}
1045
1046void cTsToPes::PutTs(const uchar *Data, int Length)
1047{
1048 if (TsError(Data)) {
1049 Reset();
1050 return; // ignore packets with TEI set, and drop any PES data collected so far
1051 }
1052 if (TsPayloadStart(Data))
1053 Reset();
1054 else if (!size)
1055 return; // skip everything before the first payload start
1056 Length = TsGetPayload(&Data);
1057 if (length + Length > size) {
1058 int NewSize = max(KILOBYTE(2), length + Length);
1059 if (uchar *NewData = (uchar *)realloc(data, NewSize)) {
1060 data = NewData;
1061 size = NewSize;
1062 }
1063 else {
1064 esyslog("ERROR: out of memory");
1065 Reset();
1066 return;
1067 }
1068 }
1069 memcpy(data + length, Data, Length);
1070 length += Length;
1071}
1072
1073#define MAXPESLENGTH 0xFFF0
1074
1075const uchar *cTsToPes::GetPes(int &Length)
1076{
1077 if (repeatLast) {
1078 repeatLast = false;
1079 Length = lastLength;
1080 return lastData;
1081 }
1082 if (offset < length && PesLongEnough(length)) {
1083 if (!PesHasLength(data)) // this is a video PES packet with undefined length
1084 offset = 6; // trigger setting PES length for initial slice
1085 if (offset) {
1086 uchar *p = data + offset - 6;
1087 if (p != data) {
1088 p -= 3;
1089 if (p < data) {
1090 Reset();
1091 return NULL;
1092 }
1093 memmove(p, data, 4);
1094 }
1095 int l = min(length - offset, MAXPESLENGTH);
1096 offset += l;
1097 if (p != data) {
1098 l += 3;
1099 p[6] = 0x80;
1100 p[7] = 0x00;
1101 p[8] = 0x00;
1102 }
1103 p[4] = l / 256;
1104 p[5] = l & 0xFF;
1105 Length = l + 6;
1106 lastLength = Length;
1107 lastData = p;
1108 return p;
1109 }
1110 else {
1111 Length = PesLength(data);
1112 if (Length <= length) {
1113 offset = Length; // to make sure we break out in case of garbage data
1114 lastLength = Length;
1115 lastData = data;
1116 return data;
1117 }
1118 }
1119 }
1120 return NULL;
1121}
1122
1124{
1125 repeatLast = true;
1126}
1127
1129{
1130 length = offset = 0;
1131 lastData = NULL;
1132 lastLength = 0;
1133 repeatLast = false;
1134}
1135
1136// --- Some helper functions for debugging -----------------------------------
1137
1138void BlockDump(const char *Name, const u_char *Data, int Length)
1139{
1140 printf("--- %s\n", Name);
1141 for (int i = 0; i < Length; i++) {
1142 if (i && (i % 16) == 0)
1143 printf("\n");
1144 printf(" %02X", Data[i]);
1145 }
1146 printf("\n");
1147}
1148
1149void TsDump(const char *Name, const u_char *Data, int Length)
1150{
1151 printf("%s: %04X", Name, Length);
1152 int n = min(Length, 20);
1153 for (int i = 0; i < n; i++)
1154 printf(" %02X", Data[i]);
1155 if (n < Length) {
1156 printf(" ...");
1157 n = max(n, Length - 10);
1158 for (n = max(n, Length - 10); n < Length; n++)
1159 printf(" %02X", Data[n]);
1160 }
1161 printf("\n");
1162}
1163
1164void PesDump(const char *Name, const u_char *Data, int Length)
1165{
1166 TsDump(Name, Data, Length);
1167}
1168
1169// --- cFrameParser ----------------------------------------------------------
1170
1172protected:
1173 bool debug;
1177 uint16_t frameWidth;
1178 uint16_t frameHeight;
1182public:
1183 cFrameParser(void);
1184 virtual ~cFrameParser() {};
1185 virtual int Parse(const uchar *Data, int Length, int Pid) = 0;
1192 void SetDebug(bool Debug) { debug = Debug; }
1193 bool NewFrame(void) { return newFrame; }
1194 bool IndependentFrame(void) { return independentFrame; }
1196 uint16_t FrameWidth(void) { return frameWidth; }
1197 uint16_t FrameHeight(void) { return frameHeight; }
1198 double FramesPerSecond(void) { return framesPerSecond; }
1199 eScanType ScanType(void) { return scanType; }
1201 };
1202
1204{
1205 debug = true;
1206 newFrame = false;
1207 independentFrame = false;
1209 frameWidth = 0;
1210 frameHeight = 0;
1211 framesPerSecond = 0.0;
1214}
1215
1216// --- cAudioParser ----------------------------------------------------------
1217
1219public:
1220 cAudioParser(void);
1221 virtual int Parse(const uchar *Data, int Length, int Pid);
1222 };
1223
1227
1228int cAudioParser::Parse(const uchar *Data, int Length, int Pid)
1229{
1230 if (TsPayloadStart(Data)) {
1231 newFrame = independentFrame = true;
1232 if (debug)
1233 dbgframes("/");
1234 }
1235 else
1236 newFrame = independentFrame = false;
1237 return TS_SIZE;
1238}
1239
1240// --- cMpeg2Parser ----------------------------------------------------------
1241
1243private:
1244 uint32_t scanner;
1248 const double frame_rate_table[9] = {
1249 0, // 0 forbidden
1250 24000./1001., // 1 23.976...
1251 24., // 2 24
1252 25., // 3 25
1253 30000./1001., // 4 29.97...
1254 30., // 5 30
1255 50., // 6 50
1256 60000./1001., // 7 59.94...
1257 60. // 8 60
1258 };
1259public:
1260 cMpeg2Parser(void);
1261 virtual int Parse(const uchar *Data, int Length, int Pid);
1262 };
1263
1265{
1267 seenIndependentFrame = false;
1268 lastIFrameTemporalReference = -1; // invalid
1269 seenScanType = false;
1270}
1271
1272int cMpeg2Parser::Parse(const uchar *Data, int Length, int Pid)
1273{
1274 newFrame = independentFrame = false;
1275 bool SeenPayloadStart = false;
1276 cTsPayload tsPayload(const_cast<uchar *>(Data), Length, Pid);
1277 if (TsPayloadStart(Data)) {
1278 SeenPayloadStart = true;
1279 tsPayload.SkipPesHeader();
1282 dbgframes("/");
1283 }
1284 uint32_t OldScanner = scanner; // need to remember it in case of multiple frames per payload
1285 for (;;) {
1286 if (!SeenPayloadStart && tsPayload.AtTsStart())
1287 OldScanner = scanner;
1288 scanner = (scanner << 8) | tsPayload.GetByte();
1289 if (scanner == 0x00000100) { // Picture Start Code
1290 if (!SeenPayloadStart && tsPayload.GetLastIndex() > TS_SIZE) {
1291 scanner = OldScanner;
1292 return tsPayload.Used() - TS_SIZE;
1293 }
1294 uchar b1 = tsPayload.GetByte();
1295 uchar b2 = tsPayload.GetByte();
1296 int TemporalReference = (b1 << 2 ) + ((b2 & 0xC0) >> 6);
1297 uchar FrameType = (b2 >> 3) & 0x07;
1298 if (tsPayload.Find(0x000001B5)) { // Extension start code
1299 if (((tsPayload.GetByte() & 0xF0) >> 4) == 0x08) { // Picture coding extension
1300 tsPayload.GetByte();
1301 uchar PictureStructure = tsPayload.GetByte() & 0x03;
1302 if (PictureStructure == 0x02) // bottom field
1303 break;
1304 }
1305 }
1306 newFrame = true;
1307 independentFrame = FrameType == 1; // I-Frame
1308 if (independentFrame) {
1311 lastIFrameTemporalReference = TemporalReference;
1312 }
1313 if (debug) {
1316 static const char FrameTypes[] = "?IPBD???";
1317 dbgframes("%c", FrameTypes[FrameType]);
1318 }
1319 }
1320 tsPayload.Statistics();
1321 break;
1322 }
1323 else if (frameWidth == 0 && scanner == 0x000001B3) { // Sequence header code
1324 frameWidth = tsPayload.GetByte() << 4;
1325 uchar b = tsPayload.GetByte(); // ignoring two MSB of width and height in sequence extension
1326 frameWidth |= b >> 4; // as 12 Bit = max 4095 should be sufficient for all available MPEG2 streams
1327 frameHeight = (b & 0x0F) << 8 | tsPayload.GetByte();
1328 b = tsPayload.GetByte(); // hi: aspect ratio info, lo: frame rate code
1329 switch (b >> 4) {
1330 case 1: aspectRatio = ar_1_1; break;
1331 case 2: aspectRatio = ar_4_3; break;
1332 case 3: aspectRatio = ar_16_9; break;
1333 case 4: aspectRatio = ar_2_21_1; break;
1334 default: aspectRatio = arUnknown;
1335 }
1336 uchar frame_rate_value = b & 0x0F;
1337 if (frame_rate_value > 0 && frame_rate_value <= 8)
1338 framesPerSecond = frame_rate_table[frame_rate_value];
1339 }
1340 else if (!seenScanType && scanner == 0x000001B5) { // Extension start code
1341 if ((tsPayload.GetByte() & 0xF0) == 0x10) { // Sequence Extension
1342 scanType = (tsPayload.GetByte() & 0x40) ? stProgressive : stInterlaced;
1343 seenScanType = true;
1344 if (debug) {
1346 dsyslog("%s", *s);
1347 dbgframes("\n%s", *s);
1348 }
1349 }
1350 }
1351 if (tsPayload.AtPayloadStart() // stop at a new payload start to have the buffer refilled if necessary
1352 || tsPayload.Eof()) // or if we're out of data
1353 break;
1354 }
1355 return tsPayload.Used();
1356}
1357
1358// --- cH264Parser -----------------------------------------------------------
1359
1361private:
1368 uchar byte; // holds the current byte value in case of bitwise access
1369 int bit; // the bit index into the current byte (-1 if we're not in bit reading mode)
1370 int zeroBytes; // the number of consecutive zero bytes (to detect 0x000003)
1371 // Identifiers written in '_' notation as in "ITU-T H.264":
1375protected:
1377 uint32_t scanner;
1380 uchar GetByte(bool Raw = false);
1384 uchar GetBit(void);
1385 uint32_t GetBits(int Bits);
1386 uint32_t GetGolombUe(void);
1387 int32_t GetGolombSe(void);
1388 void ParseAccessUnitDelimiter(void);
1389 void ParseSequenceParameterSet(void);
1390 void ParseSliceHeader(void);
1391public:
1392 cH264Parser(void);
1396 virtual int Parse(const uchar *Data, int Length, int Pid);
1397 };
1398
1400{
1401 byte = 0;
1402 bit = -1;
1403 zeroBytes = 0;
1407 frame_mbs_only_flag = false;
1408 gotAccessUnitDelimiter = false;
1410}
1411
1413{
1414 uchar b = tsPayload.GetByte();
1415 if (!Raw) {
1416 // If we encounter the byte sequence 0x000003, we need to skip the 0x03:
1417 if (b == 0x00)
1418 zeroBytes++;
1419 else {
1420 if (b == 0x03 && zeroBytes >= 2)
1421 b = tsPayload.GetByte();
1422 zeroBytes = b ? 0 : 1;
1423 }
1424 }
1425 else
1426 zeroBytes = 0;
1427 bit = -1;
1428 return b;
1429}
1430
1432{
1433 if (bit < 0) {
1434 byte = GetByte();
1435 bit = 7;
1436 }
1437 return (byte & (1 << bit--)) ? 1 : 0;
1438}
1439
1440uint32_t cH264Parser::GetBits(int Bits)
1441{
1442 uint32_t b = 0;
1443 while (Bits--)
1444 b |= GetBit() << Bits;
1445 return b;
1446}
1447
1449{
1450 int z = -1;
1451 for (int b = 0; !b && z < 32; z++) // limiting z to no get stuck if GetBit() always returns 0
1452 b = GetBit();
1453 return (1 << z) - 1 + GetBits(z);
1454}
1455
1457{
1458 uint32_t v = GetGolombUe();
1459 if (v) {
1460 if ((v & 0x01) != 0)
1461 return (v + 1) / 2; // fails for v == 0xFFFFFFFF, but that will probably never happen
1462 else
1463 return -int32_t(v / 2);
1464 }
1465 return v;
1466}
1467
1468int cH264Parser::Parse(const uchar *Data, int Length, int Pid)
1469{
1470 newFrame = independentFrame = false;
1471 tsPayload.Setup(const_cast<uchar *>(Data), Length, Pid);
1472 if (TsPayloadStart(Data)) {
1476 dbgframes("/");
1477 }
1478 }
1479 for (;;) {
1480 scanner = (scanner << 8) | GetByte(true);
1481 if ((scanner & 0xFFFFFF00) == 0x00000100) { // NAL unit start
1482 uchar NalUnitType = scanner & 0x1F;
1483 switch (NalUnitType) {
1486 break;
1490 }
1491 break;
1495 gotAccessUnitDelimiter = false;
1496 if (newFrame)
1498 return tsPayload.Used();
1499 }
1500 break;
1501 default: ;
1502 }
1503 }
1504 if (tsPayload.AtPayloadStart() // stop at a new payload start to have the buffer refilled if necessary
1505 || tsPayload.Eof()) // or if we're out of data
1506 break;
1507 }
1508 return tsPayload.Used();
1509}
1510
1512{
1514 dbgframes("A");
1515 GetByte(); // primary_pic_type
1516}
1517
1519{
1520 int chroma_format_idc = 0;
1521 int bitDepth = 0;
1522 uchar profile_idc = GetByte(); // profile_idc
1523 GetByte(); // constraint_set[0-5]_flags, reserved_zero_2bits
1524 GetByte(); // level_idc
1525 GetGolombUe(); // seq_parameter_set_id
1526 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) {
1527 chroma_format_idc = GetGolombUe(); // chroma_format_idc
1528 if (chroma_format_idc == 3)
1530 bitDepth = 8 + GetGolombUe(); // bit_depth_luma_minus8
1531 GetGolombUe(); // bit_depth_chroma_minus8
1532 GetBit(); // qpprime_y_zero_transform_bypass_flag
1533 if (GetBit()) { // seq_scaling_matrix_present_flag
1534 for (int i = 0; i < ((chroma_format_idc != 3) ? 8 : 12); i++) {
1535 if (GetBit()) { // seq_scaling_list_present_flag
1536 int SizeOfScalingList = (i < 6) ? 16 : 64;
1537 int LastScale = 8;
1538 int NextScale = 8;
1539 for (int j = 0; j < SizeOfScalingList; j++) {
1540 if (NextScale)
1541 NextScale = (LastScale + GetGolombSe() + 256) % 256; // delta_scale
1542 if (NextScale)
1543 LastScale = NextScale;
1544 }
1545 }
1546 }
1547 }
1548 }
1549 log2_max_frame_num = GetGolombUe() + 4; // log2_max_frame_num_minus4
1550 int pic_order_cnt_type = GetGolombUe(); // pic_order_cnt_type
1551 if (pic_order_cnt_type == 0)
1552 GetGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
1553 else if (pic_order_cnt_type == 1) {
1554 GetBit(); // delta_pic_order_always_zero_flag
1555 GetGolombSe(); // offset_for_non_ref_pic
1556 GetGolombSe(); // offset_for_top_to_bottom_field
1557 for (int i = GetGolombUe(); i--; ) // num_ref_frames_in_pic_order_cnt_cycle
1558 GetGolombSe(); // offset_for_ref_frame
1559 }
1560 GetGolombUe(); // max_num_ref_frames
1561 GetBit(); // gaps_in_frame_num_value_allowed_flag
1562 uint16_t frame_Width = 16 * (1 + GetGolombUe()); // pic_width_in_mbs_minus1
1563 uint16_t frame_Height = 16 * (1 + GetGolombUe()); // pic_height_in_map_units_minus1
1564 frame_mbs_only_flag = GetBit(); // frame_mbs_only_flag
1565 if (frameWidth == 0) {
1567 if (!frame_mbs_only_flag) {
1568 GetBit(); // mb_adaptive_frame_field_flag
1569 frame_Height *= 2;
1570 }
1571 GetBit(); // direct_8x8_inference_flag
1572 bool frame_cropping_flag = GetBit(); // frame_cropping_flag
1573 if (frame_cropping_flag) {
1574 uint16_t frame_crop_left_offset = GetGolombUe(); // frame_crop_left_offset
1575 uint16_t frame_crop_right_offset = GetGolombUe(); // frame_crop_right_offset
1576 uint16_t frame_crop_top_offset = GetGolombUe(); // frame_crop_top_offset
1577 uint16_t frame_crop_bottom_offset = GetGolombUe(); // frame_crop_bottom_offset
1578 uint16_t CropUnitX = 1;
1579 uint16_t CropUnitY = frame_mbs_only_flag ? 1 : 2;
1580 if (!separate_colour_plane_flag && chroma_format_idc > 0) {
1581 if (chroma_format_idc == 1) {
1582 CropUnitX = 2;
1583 CropUnitY *= 2;
1584 }
1585 else if (chroma_format_idc == 2)
1586 CropUnitX = 2;
1587 }
1588 frame_Width -= CropUnitX * (frame_crop_left_offset + frame_crop_right_offset);
1589 frame_Height -= CropUnitY * (frame_crop_top_offset + frame_crop_bottom_offset);
1590 if (frame_Height > 1080 && frame_Height <= 1090) // workaround for channels with wrong crop parameters
1591 frame_Height = 1080;
1592 }
1593 frameWidth = frame_Width;
1594 frameHeight = frame_Height;
1595 // VUI parameters
1596 if (GetBit()) { // vui_parameters_present_flag
1597 if (GetBit()) { // aspect_ratio_info_present
1598 int aspect_ratio_idc = GetBits(8); // aspect_ratio_idc
1599 if (aspect_ratio_idc == 255) // EXTENDED_SAR
1600 GetBits(32); // sar_width, sar_height
1601 else if (frameHeight >= 720 && (aspect_ratio_idc == 1 || aspect_ratio_idc == 14 || aspect_ratio_idc == 15 || aspect_ratio_idc == 16))
1603 // implement decoding of other aspect_ratio_idc values when they are required
1604 }
1605 if (GetBit()) // overscan_info_present_flag
1606 GetBit(); // overscan_approriate_flag
1607 if (GetBit()) { // video_signal_type_present_flag
1608 GetBits(4); // video_format, video_full_range_flag
1609 if (GetBit()) // colour_description_present_flag
1610 GetBits(24); // colour_primaries, transfer_characteristics, matrix_coefficients
1611 }
1612 if (GetBit()) { // chroma_loc_info_present_flag
1613 GetGolombUe(); // chroma_sample_loc_type_top_field
1614 GetGolombUe(); // chroma_sample_loc_type_bottom_field
1615 }
1616 if (GetBit()) { // timing_info_present_flag
1617 uint32_t num_units_in_tick = GetBits(32); // num_units_in_tick
1618 uint32_t time_scale = GetBits(32); // time_scale
1619 if (num_units_in_tick > 0)
1620 framesPerSecond = double(time_scale) / (num_units_in_tick << 1);
1621 }
1622 }
1623 if (debug) {
1624 cString s = cString::sprintf("H.264: %d x %d%c %.2f fps %d Bit %s", frameWidth, frameHeight, ScanTypeChars[scanType], framesPerSecond, bitDepth, AspectRatioTexts[aspectRatio]);
1625 dsyslog("%s", *s);
1626 dbgframes("\n%s", *s);
1627 }
1628 }
1629 if (debug) {
1631 dbgframes("A"); // just for completeness
1632 dbgframes(frame_mbs_only_flag ? "S" : "s");
1633 }
1634}
1635
1637{
1638 newFrame = true;
1639 GetGolombUe(); // first_mb_in_slice
1640 int slice_type = GetGolombUe(); // slice_type, 0 = P, 1 = B, 2 = I, 3 = SP, 4 = SI
1641 independentFrame = (slice_type % 5) == 2;
1642 if (debug) {
1643 static const char SliceTypes[] = "PBIpi";
1644 dbgframes("%c", SliceTypes[slice_type % 5]);
1645 }
1647 return; // don't need the rest - a frame is complete
1648 GetGolombUe(); // pic_parameter_set_id
1650 GetBits(2); // colour_plane_id
1651 GetBits(log2_max_frame_num); // frame_num
1652 if (!frame_mbs_only_flag) {
1653 if (GetBit()) // field_pic_flag
1654 newFrame = !GetBit(); // bottom_field_flag
1655 if (debug)
1656 dbgframes(newFrame ? "t" : "b");
1657 }
1658}
1659
1660// --- cH265Parser -----------------------------------------------------------
1661
1700
1702:cH264Parser()
1703{
1704}
1705
1706int cH265Parser::Parse(const uchar *Data, int Length, int Pid)
1707{
1708 newFrame = independentFrame = false;
1709 tsPayload.Setup(const_cast<uchar *>(Data), Length, Pid);
1710 if (TsPayloadStart(Data)) {
1713 }
1714 for (;;) {
1715 scanner = (scanner << 8) | GetByte(true);
1716 if ((scanner & 0xFFFFFF00) == 0x00000100) { // NAL unit start
1717 uchar NalUnitType = (scanner >> 1) & 0x3F;
1718 GetByte(); // nuh_layer_id + nuh_temporal_id_plus1
1719 if (NalUnitType <= nutSliceSegmentRASLR || (NalUnitType >= nutSliceSegmentBLAWLP && NalUnitType <= nutSliceSegmentCRANUT)) {
1720 if (NalUnitType == nutSliceSegmentIDRWRADL || NalUnitType == nutSliceSegmentIDRNLP || NalUnitType == nutSliceSegmentCRANUT)
1721 independentFrame = true;
1722 if (GetBit()) { // first_slice_segment_in_pic_flag
1723 newFrame = true;
1725 }
1726 break;
1727 }
1728 else if (frameWidth == 0 && NalUnitType == nutSequenceParameterSet) {
1731 }
1732 }
1733 if (tsPayload.AtPayloadStart() // stop at a new payload start to have the buffer refilled if necessary
1734 || tsPayload.Eof()) // or if we're out of data
1735 break;
1736 }
1737 return tsPayload.Used();
1738}
1739
1741{
1743 uint8_t sub_layer_profile_present_flag[8];
1744 uint8_t sub_layer_level_present_flag[8];
1745 GetBits(4); // sps_video_parameter_set_id
1746 int sps_max_sub_layers_minus1 = GetBits(3); // sps_max_sub_layers_minus1
1747 GetBit(); // sps_temporal_id_nesting_flag
1748 // begin profile_tier_level(1, sps_max_sub_layers_minus1)
1749 GetByte();
1750 GetByte();
1751 GetByte();
1752 GetByte();
1753 GetByte();
1754 bool general_progressive_source_flag = GetBit(); // general_progressive_source_flag
1755 scanType = general_progressive_source_flag ? stProgressive : stInterlaced;
1756 GetBit(); // general_interlaced_source_flag
1757 GetBits(6);
1758 GetByte();
1759 GetByte();
1760 GetByte();
1761 GetByte();
1762 GetByte();
1763 GetByte(); // general_level_idc
1764 for (int i = 0; i < sps_max_sub_layers_minus1; i++ ) {
1765 sub_layer_profile_present_flag[i] = GetBit(); // sub_layer_profile_present_flag[i]
1766 sub_layer_level_present_flag[i] = GetBit(); // sub_layer_level_present_flag[i]
1767 }
1768 if (sps_max_sub_layers_minus1 > 0) {
1769 for (int i = sps_max_sub_layers_minus1; i < 8; i++ )
1770 GetBits(2); // reserved_zero_2bits[i]
1771 }
1772 for (int i = 0; i < sps_max_sub_layers_minus1; i++ ) {
1773 if (sub_layer_profile_present_flag[i] )
1774 GetBits(88);
1775 if (sub_layer_level_present_flag[i])
1776 GetBits(8);
1777 }
1778 // end profile_tier_level
1779 GetGolombUe(); // sps_seq_parameter_set_id
1780 int chroma_format_idc = GetGolombUe(); // chroma_format_idc
1781 if (chroma_format_idc == 3)
1782 separate_colour_plane_flag = GetBit(); // separate_colour_plane_flag
1783 frameWidth = GetGolombUe(); // pic_width_in_luma_samples
1784 frameHeight = GetGolombUe(); // pic_height_in_luma_samples
1785 bool conformance_window_flag = GetBit(); // conformance_window_flag
1786 if (conformance_window_flag) {
1787 int conf_win_left_offset = GetGolombUe(); // conf_win_left_offset
1788 int conf_win_right_offset = GetGolombUe(); // conf_win_right_offset
1789 int conf_win_top_offset = GetGolombUe(); // conf_win_top_offset
1790 int conf_win_bottom_offset = GetGolombUe(); // conf_win_bottom_offset
1791 uint16_t SubWidthC = 1;
1792 uint16_t SubHeightC = 1;
1793 if (!separate_colour_plane_flag && chroma_format_idc > 0) {
1794 if (chroma_format_idc == 1) {
1795 SubWidthC = 2;
1796 SubHeightC = 2;
1797 }
1798 else if (chroma_format_idc == 2)
1799 SubWidthC = 2;
1800 }
1801 frameWidth -= SubWidthC * (conf_win_left_offset + conf_win_right_offset);
1802 frameHeight -= SubHeightC * (conf_win_top_offset + conf_win_bottom_offset);
1803 }
1804 int bitDepth = 8 + GetGolombUe(); // bit_depth_luma_minus8
1805 GetGolombUe(); // bit_depth_chroma_minus8
1806 int log2_max_pic_order_cnt_lsb_minus4 = GetGolombUe(); // log2_max_pic_order_cnt_lsb_minus4
1807 int sps_sub_layer_ordering_info_present_flag = GetBit(); // sps_sub_layer_ordering_info_present_flag
1808 for (int i = sps_sub_layer_ordering_info_present_flag ? 0 : sps_max_sub_layers_minus1; i <= sps_max_sub_layers_minus1; ++i) {
1809 GetGolombUe(); // sps_max_dec_pic_buffering_minus1[i]
1810 GetGolombUe(); // sps_max_num_reorder_pics[i]
1811 GetGolombUe(); // sps_max_latency_increase_plus1[i]
1812 }
1813 GetGolombUe(); // log2_min_luma_coding_block_size_minus3
1814 GetGolombUe(); // log2_diff_max_min_luma_coding_block_size
1815 GetGolombUe(); // log2_min_luma_transform_block_size_minus2
1816 GetGolombUe(); // log2_diff_max_min_luma_transform_block_size
1817 GetGolombUe(); // max_transform_hierarchy_depth_inter
1818 GetGolombUe(); // max_transform_hierarchy_depth_intra
1819 if (GetBit()) { // scaling_list_enabled_flag
1820 if (GetBit()) { // sps_scaling_list_data_present_flag
1821 // begin scaling_list_data
1822 for (int sizeId = 0; sizeId < 4; ++sizeId) {
1823 for (int matrixId = 0; matrixId < 6; matrixId += (sizeId == 3) ? 3 : 1) {
1824 if (!GetBit()) // scaling_list_pred_mode_flag[sizeId][matrixId]
1825 GetGolombUe(); // scaling_list_pred_matrix_id_delta[sizeId][matrixId]
1826 else {
1827 int coefNum = min(64, (1 << (4 + (sizeId << 1))));
1828 if (sizeId > 1)
1829 GetGolombSe(); // scaling_list_dc_coef_minus8[sizeId−2][matrixId]
1830 for (int i = 0; i < coefNum; ++i)
1831 GetGolombSe(); // scaling_list_delta_coef
1832 }
1833 }
1834 }
1835 }
1836 // end scaling_list_data
1837 }
1838 GetBits(2); // amp_enabled_flag, sample_adaptive_offset_enabled_flag
1839 if (GetBit()) { // pcm_enabled_flag
1840 GetBits(8); // pcm_sample_bit_depth_luma_minus1, pcm_sample_bit_depth_chroma_minus1
1841 GetGolombUe(); // log2_min_pcm_luma_coding_block_size_minus3
1842 GetGolombUe(); // log2_diff_max_min_pcm_luma_coding_block_size
1843 GetBit(); // pcm_loop_filter_disabled_flag
1844 }
1845 uint32_t num_short_term_ref_pic_sets = GetGolombUe(); // num_short_term_ref_pic_sets
1846 uint32_t NumDeltaPocs[num_short_term_ref_pic_sets];
1847 for (uint32_t stRpsIdx = 0; stRpsIdx < num_short_term_ref_pic_sets; ++stRpsIdx) {
1848 // start of st_ref_pic_set(stRpsIdx)
1849 bool inter_ref_pic_set_prediction_flag = false;
1850 if (stRpsIdx != 0)
1851 inter_ref_pic_set_prediction_flag = GetBit(); // inter_ref_pic_set_prediction_flag
1852 if (inter_ref_pic_set_prediction_flag) {
1853 uint32_t RefRpsIdx, delta_idx_minus1 = 0;
1854 if (stRpsIdx == num_short_term_ref_pic_sets)
1855 delta_idx_minus1 = GetGolombUe(); // delta_idx_minus1
1856 GetBit(); // delta_rps_sign
1857 GetGolombUe(); // abs_delta_rps_minus1
1858 RefRpsIdx = stRpsIdx - (delta_idx_minus1 + 1);
1859 NumDeltaPocs[stRpsIdx] = 0;
1860 for (uint32_t j = 0; j <= NumDeltaPocs[RefRpsIdx]; ++j) {
1861 if (!GetBit()) { // used_by_curr_pic_flag[j]
1862 if (GetBit()) // use_delta_flag[j]
1863 NumDeltaPocs[stRpsIdx]++;
1864 }
1865 else
1866 NumDeltaPocs[stRpsIdx]++;
1867 }
1868 }
1869 else {
1870 uint32_t num_negative_pics = GetGolombUe(); // num_negative_pics
1871 uint32_t num_positive_pics = GetGolombUe(); // num_positive_pics
1872 for (uint32_t j = 0; j < num_negative_pics; ++j) {
1873 GetGolombUe(); // delta_poc_s0_minus1[i]
1874 GetBit(); // used_by_curr_pic_s0_flag[i]
1875 }
1876 for (uint32_t j = 0; j < num_positive_pics; ++j) {
1877 GetGolombUe(); // delta_poc_s1_minus1[i]
1878 GetBit(); // delta_poc_s1_minus1[i]
1879 }
1880 NumDeltaPocs[stRpsIdx] = num_negative_pics + num_positive_pics;
1881 }
1882 // end of st_ref_pic_set(stRpsIdx)
1883 }
1884 if (GetBit()) { // long_term_ref_pics_present_flag
1885 uint32_t num_long_term_ref_pics_sps = GetGolombUe(); // num_long_term_ref_pics_sps
1886 for (uint32_t i = 0; i < num_long_term_ref_pics_sps; ++i) {
1887 GetBits(log2_max_pic_order_cnt_lsb_minus4 + 4); // lt_ref_pic_poc_lsb_sps[i]
1888 GetBit(); // used_by_curr_pic_lt_sps_flag[i]
1889 }
1890 }
1891 GetBits(2); // sps_temporal_mvp_enabled_flag, strong_intra_smoothing_enabled_flag
1892 if (GetBit()) { // vui_parameters_present_flag
1893 // begin of vui_parameters()
1894 if (GetBit()) { // aspect_ratio_info_present_flag
1895 int aspect_ratio_idc = GetBits(8); // aspect_ratio_idc
1896 if (aspect_ratio_idc == 255) // EXTENDED_SAR
1897 GetBits(32); // sar_width, sar_height
1898 else if (aspect_ratio_idc == 1 || aspect_ratio_idc == 14)
1900 // implement decoding of other aspect_ratio_idc values when they are required
1901 }
1902 if (GetBit()) // overscan_info_present_flag
1903 GetBit(); // overscan_appropriate_flag
1904 if (GetBit()) { // video_signal_type_present_flag
1905 GetBits(4); // video_format, video_full_range_flag
1906 if (GetBit()) // colour_description_present_flag
1907 GetBits(24); // colour_primaries, transfer_characteristics, matrix_coeffs
1908 }
1909 if (GetBit()) { // chroma_loc_info_present_flag
1910 GetGolombUe(); // chroma_sample_loc_type_top_field
1911 GetGolombUe(); // chroma_sample_loc_type_bottom_field
1912 }
1913 GetBits(3); // neutral_chroma_indication_flag, field_seq_flag, frame_field_info_present_flag
1914 if (GetBit()) { // default_display_window_flag
1915 GetGolombUe(); // def_disp_win_left_offset
1916 GetGolombUe(); // def_disp_win_right_offset
1917 GetGolombUe(); // def_disp_win_top_offset
1918 GetGolombUe(); // def_disp_win_bottom_offset
1919 }
1920 if (GetBit()) { // vui_timing_info_present_flag
1921 uint32_t vui_num_units_in_tick = GetBits(32); // vui_num_units_in_tick
1922 uint32_t vui_time_scale = GetBits(32); // vui_time_scale
1923 if (vui_num_units_in_tick > 0)
1924 framesPerSecond = (double)vui_time_scale / vui_num_units_in_tick;
1925 }
1926 }
1927 if (debug) {
1928 cString s = cString::sprintf("H.265: %d x %d%c %.2f fps %d Bit %s", frameWidth, frameHeight, ScanTypeChars[scanType], framesPerSecond, bitDepth, AspectRatioTexts[aspectRatio]);
1929 dsyslog("%s", *s);
1930 dbgframes("\n%s", *s);
1931 }
1932}
1933
1934// --- cFrameDetector --------------------------------------------------------
1935
1936const char *ScanTypeChars = "-pi"; // index is eScanType
1937const char *AspectRatioTexts[] = { // index is eAspectRatio
1938 "-",
1939 "1:1",
1940 "4:3",
1941 "16:9",
1942 "2.21:1",
1943 NULL
1944 };
1945
1947{
1948 parser = NULL;
1949 SetPid(Pid, Type);
1950 synced = false;
1951 newFrame = independentFrame = false;
1952 numPtsValues = 0;
1953 numIFrames = 0;
1954 framesPerSecond = 0;
1955 frameWidth = 0;
1956 frameHeight = 0;
1960 scanning = false;
1961}
1962
1963static int CmpUint32(const void *p1, const void *p2)
1964{
1965 if (*(uint32_t *)p1 < *(uint32_t *)p2) return -1;
1966 if (*(uint32_t *)p1 > *(uint32_t *)p2) return 1;
1967 return 0;
1968}
1969
1970void cFrameDetector::SetPid(int Pid, int Type)
1971{
1972 pid = Pid;
1973 type = Type;
1974 isVideo = type == 0x01 || type == 0x02 || type == 0x1B || type == 0x24; // MPEG 1, 2, H.264 or H.265
1975 delete parser;
1976 parser = NULL;
1977 if (type == 0x01 || type == 0x02)
1978 parser = new cMpeg2Parser;
1979 else if (type == 0x1B)
1980 parser = new cH264Parser;
1981 else if (type == 0x24)
1982 parser = new cH265Parser;
1983 else if (type == 0x03 || type == 0x04 || type == 0x06) // MPEG audio or AC3 audio
1984 parser = new cAudioParser;
1985 else if (type != 0)
1986 esyslog("ERROR: unknown stream type %d (PID %d) in frame detector", type, pid);
1987}
1988
1989int cFrameDetector::Analyze(const uchar *Data, int Length)
1990{
1991 if (!parser)
1992 return 0;
1993 int Processed = 0;
1994 newFrame = independentFrame = false;
1995 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
1996 // Sync on TS packet borders:
1997 if (int Skipped = TS_SYNC(Data, Length))
1998 return Processed + Skipped;
1999 // Handle one TS packet:
2000 int Handled = TS_SIZE;
2001 if (TsHasPayload(Data) && !TsIsScrambled(Data)) {
2002 int Pid = TsPid(Data);
2003 if (Pid == pid) {
2004 if (Processed)
2005 return Processed;
2006 if (TsPayloadStart(Data))
2007 scanning = true;
2008 if (scanning) {
2009 // Detect the beginning of a new frame:
2010 if (TsPayloadStart(Data)) {
2013 }
2014 int n = parser->Parse(Data, Length, pid);
2015 if (n > 0) {
2016 if (parser->NewFrame()) {
2017 newFrame = true;
2019 if (synced) {
2020 if (framesPerPayloadUnit <= 1)
2021 scanning = false;
2022 }
2023 else {
2024 if (parser->FramesPerSecond() > 0.0) {
2030 synced = true;
2031 parser->SetDebug(false);
2032 }
2034 if (independentFrame)
2035 numIFrames++;
2036 }
2037 }
2038 Handled = n;
2039 }
2040 }
2041 if (TsPayloadStart(Data)) {
2042 // Determine the frame rate from the PTS values in the PES headers:
2043 if (framesPerSecond <= 0.0) {
2044 // frame rate unknown, so collect a sequence of PTS values:
2045 if (numPtsValues < 2 || numPtsValues < MaxPtsValues && numIFrames < 2) { // collect a sequence containing at least two I-frames
2046 if (newFrame) { // only take PTS values at the beginning of a frame (in case if fields!)
2047 const uchar *Pes = Data + TsPayloadOffset(Data);
2048 if (numIFrames && PesHasPts(Pes)) {
2050 // check for rollover:
2051 if (numPtsValues && ptsValues[numPtsValues - 1] > 0xF0000000 && ptsValues[numPtsValues] < 0x10000000) {
2052 dbgframes("#");
2053 numPtsValues = 0;
2054 numIFrames = 0;
2055 }
2056 else
2057 numPtsValues++;
2058 }
2059 }
2060 }
2061 if (numPtsValues >= 2 && numIFrames >= 2) {
2062 // find the smallest PTS delta:
2063 qsort(ptsValues, numPtsValues, sizeof(uint32_t), CmpUint32);
2064 numPtsValues--;
2065 for (int i = 0; i < numPtsValues; i++)
2066 ptsValues[i] = ptsValues[i + 1] - ptsValues[i];
2067 qsort(ptsValues, numPtsValues, sizeof(uint32_t), CmpUint32);
2068 int Div = framesPerPayloadUnit;
2069 if (framesPerPayloadUnit > 1)
2071 if (Div <= 0)
2072 Div = 1;
2073 int Delta = ptsValues[0] / Div;
2074 // determine frame info:
2075 if (isVideo) {
2076 if (Delta == 3753)
2077 framesPerSecond = 24.0 / 1.001;
2078 else if (abs(Delta - 3600) <= 1)
2079 framesPerSecond = 25.0;
2080 else if (Delta % 3003 == 0)
2081 framesPerSecond = 30.0 / 1.001;
2082 else if (abs(Delta - 1800) <= 1)
2083 framesPerSecond = 50.0;
2084 else if (Delta == 1501)
2085 framesPerSecond = 60.0 / 1.001;
2086 else {
2088 dsyslog("unknown frame delta (%d), assuming %5.2f fps", Delta, DEFAULTFRAMESPERSECOND);
2089 }
2090 }
2091 else // audio
2092 framesPerSecond = double(PTSTICKS) / Delta; // PTS of audio frames is always increasing
2093 dbgframes("\nDelta = %d FPS = %5.2f FPPU = %d NF = %d TRO = %d\n", Delta, framesPerSecond, framesPerPayloadUnit, numPtsValues + 1, parser->IFrameTemporalReferenceOffset());
2094 synced = true;
2095 parser->SetDebug(false);
2096 }
2097 }
2098 }
2099 }
2100 else if (Pid == PATPID && synced && Processed)
2101 return Processed; // allow the caller to see any PAT packets
2102 }
2103 Data += Handled;
2104 Length -= Handled;
2105 Processed += Handled;
2106 if (newFrame)
2107 break;
2108 }
2109 return Processed;
2110}
#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)
Parses the given Data, which is a sequence of Length bytes of TS packets.
Definition remux.c:1228
cAudioParser(void)
Definition remux.c:1224
const int * Dpids(void) const
Definition channels.h:157
uint16_t AncillaryPageId(int i) const
Definition channels.h:169
uint16_t CompositionPageId(int i) const
Definition channels.h:168
int Tpid(void) const
Definition channels.h:170
const char * Slang(int i) const
Definition channels.h:164
int Vpid(void) const
Definition channels.h:153
int Atype(int i) const
Definition channels.h:165
int Dtype(int i) const
Definition channels.h:166
int Dpid(int i) const
Definition channels.h:160
int Vtype(void) const
Definition channels.h:155
int Apid(int i) const
Definition channels.h:159
int Ppid(void) const
Definition channels.h:154
uchar SubtitlingType(int i) const
Definition channels.h:167
const char * Dlang(int i) const
Definition channels.h:163
int Spid(int i) const
Definition channels.h:161
const int * Apids(void) const
Definition channels.h:156
const char * Alang(int i) const
Definition channels.h:162
const int * Spids(void) const
Definition channels.h:158
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:1179
void ClrAvailableTracks(bool DescriptionsOnly=false, bool IdsOnly=false)
Clears the list of currently available tracks.
Definition device.c:1056
void EnsureSubtitleTrack(void)
Makes sure one of the preferred language subtitle tracks is selected.
Definition device.c:1212
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:1079
uchar eit[TS_SIZE]
Definition remux.h:434
cEitGenerator(int Sid=0)
Definition remux.c:947
int counter
Definition remux.h:435
uchar * AddParentalRatingDescriptor(uchar *p, uchar ParentalRating=0)
Definition remux.c:961
uint16_t YMDtoMJD(int Y, int M, int D)
Definition remux.c:955
uchar * Generate(int Sid)
Definition remux.c:972
int version
Definition remux.h:436
uint32_t ptsValues[MaxPtsValues]
Definition remux.h:534
bool synced
Definition remux.h:531
uint16_t frameHeight
Definition remux.h:540
uint16_t frameWidth
Definition remux.h:539
bool scanning
Definition remux.h:546
int framesPerPayloadUnit
Definition remux.h:544
double framesPerSecond
Definition remux.h:538
int framesInPayloadUnit
Definition remux.h:543
int numIFrames
Definition remux.h:536
bool independentFrame
Definition remux.h:533
cFrameDetector(int Pid=0, int Type=0)
Sets up a frame detector for the given Pid and stream Type.
Definition remux.c:1946
eScanType scanType
Definition remux.h:541
bool newFrame
Definition remux.h:532
cFrameParser * parser
Definition remux.h:547
int numPtsValues
Definition remux.h:535
int Analyze(const uchar *Data, int Length)
Analyzes the TS packets pointed to by Data.
Definition remux.c:1989
bool isVideo
Definition remux.h:537
void SetPid(int Pid, int Type)
Sets the Pid and stream Type to detect frames for.
Definition remux.c:1970
eAspectRatio aspectRatio
Definition remux.h:542
uint16_t FrameWidth(void)
Definition remux.c:1196
void SetDebug(bool Debug)
Definition remux.c:1192
bool NewFrame(void)
Definition remux.c:1193
eAspectRatio aspectRatio
Definition remux.c:1181
double FramesPerSecond(void)
Definition remux.c:1198
bool IndependentFrame(void)
Definition remux.c:1194
bool newFrame
Definition remux.c:1174
int iFrameTemporalReferenceOffset
Definition remux.c:1176
double framesPerSecond
Definition remux.c:1179
eScanType ScanType(void)
Definition remux.c:1199
bool independentFrame
Definition remux.c:1175
virtual ~cFrameParser()
Definition remux.c:1184
uint16_t frameHeight
Definition remux.c:1178
bool debug
Definition remux.c:1173
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:1200
eScanType scanType
Definition remux.c:1180
uint16_t FrameHeight(void)
Definition remux.c:1197
int IFrameTemporalReferenceOffset(void)
Definition remux.c:1195
uint16_t frameWidth
Definition remux.c:1177
cFrameParser(void)
Definition remux.c:1203
uint32_t GetBits(int Bits)
Definition remux.c:1440
cTsPayload tsPayload
Definition remux.c:1376
void ParseAccessUnitDelimiter(void)
Definition remux.c:1511
@ nutSequenceParameterSet
Definition remux.c:1365
@ nutCodedSliceNonIdr
Definition remux.c:1363
@ nutAccessUnitDelimiter
Definition remux.c:1366
@ nutCodedSliceIdr
Definition remux.c:1364
bool separate_colour_plane_flag
Definition remux.c:1372
bool gotAccessUnitDelimiter
Definition remux.c:1378
int zeroBytes
Definition remux.c:1370
bool frame_mbs_only_flag
Definition remux.c:1374
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:1468
int log2_max_frame_num
Definition remux.c:1373
uint32_t GetGolombUe(void)
Definition remux.c:1448
uchar GetByte(bool Raw=false)
Gets the next data byte.
Definition remux.c:1412
void ParseSliceHeader(void)
Definition remux.c:1636
void ParseSequenceParameterSet(void)
Definition remux.c:1518
uchar byte
Definition remux.c:1368
uint32_t scanner
Definition remux.c:1377
bool gotSequenceParameterSet
Definition remux.c:1379
int32_t GetGolombSe(void)
Definition remux.c:1456
cH264Parser(void)
Sets up a new H.264 parser.
Definition remux.c:1399
uchar GetBit(void)
Definition remux.c:1431
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:1706
cH265Parser(void)
Definition remux.c:1701
@ nutSliceSegmentIDRWRADL
Definition remux.c:1678
@ nutUnspecified0
Definition remux.c:1692
@ nutSliceSegmentSTSAN
Definition remux.c:1669
@ nutSliceSegmentRADLN
Definition remux.c:1671
@ nutSliceSegmentIDRNLP
Definition remux.c:1679
@ nutPrefixSEI
Definition remux.c:1688
@ nutAccessUnitDelimiter
Definition remux.c:1684
@ nutUnspecified7
Definition remux.c:1693
@ nutSliceSegmentBLAWRADL
Definition remux.c:1676
@ nutSliceSegmentTSAR
Definition remux.c:1668
@ nutSliceSegmentRADLR
Definition remux.c:1672
@ nutSliceSegmentTrailingR
Definition remux.c:1666
@ nutVideoParameterSet
Definition remux.c:1681
@ nutSequenceParameterSet
Definition remux.c:1682
@ nutSliceSegmentTSAN
Definition remux.c:1667
@ nutSliceSegmentRASLN
Definition remux.c:1673
@ nutSuffixSEI
Definition remux.c:1689
@ nutSliceSegmentBLAWLP
Definition remux.c:1675
@ nutEndOfBitstream
Definition remux.c:1686
@ nutSliceSegmentBLANLP
Definition remux.c:1677
@ nutSliceSegmentRASLR
Definition remux.c:1674
@ nutPictureParameterSet
Definition remux.c:1683
@ nutNonVCLRes3
Definition remux.c:1691
@ nutSliceSegmentCRANUT
Definition remux.c:1680
@ nutSliceSegmentTrailingN
Definition remux.c:1665
@ nutNonVCLRes0
Definition remux.c:1690
@ nutFillerData
Definition remux.c:1687
@ nutSliceSegmentSTSAR
Definition remux.c:1670
@ nutEndOfSequence
Definition remux.c:1685
void ParseSequenceParameterSet(void)
Definition remux.c:1740
bool seenIndependentFrame
Definition remux.c:1245
cMpeg2Parser(void)
Definition remux.c:1264
bool seenScanType
Definition remux.c:1247
int lastIFrameTemporalReference
Definition remux.c:1246
uint32_t scanner
Definition remux.c:1244
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:1272
const double frame_rate_table[9]
Definition remux.c:1248
int MakeCRC(uchar *Target, const uchar *Data, int Length)
Definition remux.c:451
uchar * GetPmt(int &Index)
Returns a pointer to the Index'th TS packet of the PMT section.
Definition remux.c:600
void SetChannel(const cChannel *Channel)
Sets the Channel for which the PAT/PMT shall be generated.
Definition remux.c:585
void IncEsInfoLength(int Length)
Definition remux.c:384
void IncCounter(int &Counter, uchar *TsPacket)
Definition remux.c:371
cPatPmtGenerator(const cChannel *Channel=NULL)
Definition remux.c:361
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:579
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:405
void GeneratePat(void)
Generates a PAT section for later use with GetPat().
Definition remux.c:481
uchar * GetPat(void)
Returns a pointer to the PAT section, which consists of exactly one TS packet.
Definition remux.c:594
uchar pmt[MAX_PMT_TS][TS_SIZE]
Definition remux.h:301
int MakeLanguageDescriptor(uchar *Target, const char *Language)
Definition remux.c:432
void GeneratePmt(const cChannel *Channel)
Generates a PMT section for the given Channel, for later use with GetPmt().
Definition remux.c:510
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:466
int MakeStream(uchar *Target, uchar Type, int Pid)
Definition remux.c:393
int MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
Definition remux.c:415
void IncVersion(int &Version)
Definition remux.c:378
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:938
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:611
void Reset(void)
Resets the parser.
Definition remux.c:617
void ParsePat(const uchar *Data, int Length)
Parses the PAT data from the single TS packet in Data.
Definition remux.c:627
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:919
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:659
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
static void SetBrokenLink(uchar *Data, int Length)
Definition remux.c:102
int UseDolbyDigital
Definition config.h:326
static cString sprintf(const char *fmt,...) __attribute__((format(printf
Definition tools.c:1173
int numPacketsPid
Definition remux.h:232
int pid
Definition remux.h:230
cTsPayload(void)
Definition remux.c:246
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:328
uchar GetByte(void)
Gets the next byte of the TS payload, skipping any intermediate TS header data.
Definition remux.c:280
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:323
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
bool SkipPesHeader(void)
Skips all bytes belonging to the PES header of the payload.
Definition remux.c:318
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:351
uchar SetEof(void)
Definition remux.c:259
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:334
void Reset(void)
Definition remux.c:265
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:311
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:1046
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:1123
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:1075
cTsToPes(void)
Definition remux.c:1034
int length
Definition remux.h:454
~cTsToPes()
Definition remux.c:1041
void Reset(void)
Resets the converter.
Definition remux.c:1128
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:286
@ 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:365
void TsSetPcr(uchar *p, int64_t Pcr)
Definition remux.c:131
const char * AspectRatioTexts[]
Definition remux.c:1937
#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:1164
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:234
const char * ScanTypeChars
Definition remux.c:1936
#define MAXPESLENGTH
Definition remux.c:1073
#define P_PMT_PID
Definition remux.c:463
void TsHidePayload(uchar *p)
Definition remux.c:121
static int CmpUint32(const void *p1, const void *p2)
Definition remux.c:1963
void PesSetDts(uchar *p, int64_t Dts)
Definition remux.c:225
#define EMPTY_SCANNER
Definition remux.c:30
static bool DebugPatPmt
Definition remux.c:20
int64_t TsGetDts(const uchar *p, int l)
Definition remux.c:173
void TsSetDts(uchar *p, int l, int64_t Dts)
Definition remux.c:200
void TsSetPts(uchar *p, int l, int64_t Pts)
Definition remux.c:186
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:216
int64_t TsGetPts(const uchar *p, int l)
Definition remux.c:160
void BlockDump(const char *Name, const u_char *Data, int Length)
Definition remux.c:1138
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:462
void TsDump(const char *Name, const u_char *Data, int Length)
Definition remux.c:1149
#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
int TsGetPayload(const uchar **p)
Definition remux.h:114
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
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_PAYLOAD_START
Definition remux.h:36
char * strn0cpy(char *dest, const char *src, size_t n)
Definition tools.c:131
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