/
mpegtables.cpp
1516 lines (1324 loc) · 43.9 KB
/
mpegtables.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// -*- Mode: c++ -*-
// Copyright (c) 2003-2004, Daniel Thor Kristjansson
#include "splicedescriptors.h"
#include "atscdescriptors.h"
#include "mythmiscutil.h" // for xml_indent
#include "mythlogging.h"
#include "mpegtables.h"
const unsigned char DEFAULT_PAT_HEADER[8] =
{
0x00, // TableID::PAT
0xb0, // Syntax indicator
0x00, // Length (set seperately)
0x00, // Transport stream ID top bits
0x00, // Transport stream ID bottom bits
0xc1, // current | reserved
0x00, // Current Section
0x00, // Last Section
};
const unsigned char DEFAULT_PMT_HEADER[12] =
{
0x02, // TableID::PMT
0xb0, // Syntax indicator
0x00, // Length (set seperately)
0x00, // MPEG Program number top bits (set seperately)
0x00, // MPEG Program number bottom bits (set seperately)
0xc1, // Version + Current/Next
0x00, // Current Section
0x00, // Last Section
0xff, 0xff, // PCR pid
0x00, 0x00, // Program Info Length
};
static const uint len_for_alloc[] =
{
TSPacket::kPayloadSize
- 1 /* for start of field pointer */
- 3 /* for data before data last byte of pes length */,
4000,
};
uint StreamID::Normalize(uint stream_id, const desc_list_t &desc,
const QString &sistandard)
{
if ((sistandard != "dvb") && (OpenCableVideo == stream_id))
return MPEG2Video;
/* normalize DVB style signalling to ATSC style signalling to make
* IsAudio work with either, see A/52:2010 A4 vs A5 */
if (MPEGDescriptor::Find(desc, DescriptorID::ac3))
return AC3Audio;
/* normalize DVB style signalling to ATSC style signalling to make
* IsAudio work with either */
if (MPEGDescriptor::Find(desc, DescriptorID::eac3))
return EAC3Audio;
QString reg;
const unsigned char *d = MPEGDescriptor::Find(
desc, DescriptorID::registration);
if (d)
{
RegistrationDescriptor rd(d);
if (rd.IsValid())
reg = rd.FormatIdentifierString();
}
/* normalize all three DTS frame sizes, via http://www.smpte-ra.org/mpegreg/mpegreg.html */
if ((reg == "DTS1") || (reg == "DTS2") || (reg == "DTS3"))
return DTSAudio;
/* normalize AC-3 signalling according to A/52:2010 A4 */
if (reg == "AC-3")
return AC3Audio;
/* normalize E-AC-3 signalling with guesswork via http://www.smpte-ra.org/mpegreg/mpegreg.html */
if (reg == "EAC3")
return EAC3Audio;
#if 0
// not needed while there is no specific stream id for these
if (MPEGDescriptor::Find(desc, DescriptorID::teletext) ||
MPEGDescriptor::Find(desc, DescriptorID::subtitling))
return stream_id;
#endif
return stream_id;
}
bool PSIPTable::HasCRC(void) const
{
bool has_crc = false;
switch (TableID())
{
// MPEG
case TableID::PAT:
case TableID::CAT:
case TableID::PMT:
has_crc = true;
break;
// case TableID::TSDT
// DVB manditory
case TableID::NIT:
case TableID::SDT:
case TableID::PF_EIT:
has_crc = true;
break;
case TableID::TDT:
has_crc = false;
break;
// DVB optional
case TableID::NITo:
case TableID::SDTo:
case TableID::BAT:
case TableID::PF_EITo:
has_crc = true;
break;
case TableID::RST:
case TableID::ST:
has_crc = false;
break;
case TableID::TOT:
has_crc = true;
break;
// case TableID::RNT:
// case TableID::CT:
// case TableID::RCT:
// case TableID::CIT:
// case TableID::MPEFEC:
case TableID::DIT:
has_crc = false;
break;
case TableID::SIT:
has_crc = true;
break;
// SCTE
case TableID::NITscte:
case TableID::NTT:
case TableID::SVCTscte:
case TableID::STTscte:
case TableID::SITscte:
has_crc = true;
break;
case TableID::ADET:
has_crc = false;
break;
// ATSC
case TableID::MGT:
case TableID::TVCT:
case TableID::CVCT:
case TableID::RRT:
case TableID::EIT:
case TableID::ETT:
case TableID::STT:
case TableID::DET:
case TableID::DST:
//case TableID::PIT:
case TableID::NRT:
case TableID::LTST:
case TableID::DCCT:
case TableID::DCCSCT:
//case TableID::SITatsc:
case TableID::AEIT:
case TableID::AETT:
case TableID::SVCT:
has_crc = true;
break;
default:
{
// DVB Longterm EIT data
if (TableID::SC_EITbeg <= TableID() &&
TableID() <= TableID::SC_EITendo)
{
has_crc = true;
}
// Dishnet Longterm EIT data
if (TableID::DN_EITbego <= TableID() &&
TableID() <= TableID::DN_EITendo)
{
has_crc = true;
}
}
break;
}
return has_crc;
}
bool PSIPTable::HasSectionNumber(void) const
{
bool has_sn = false;
switch (TableID())
{
// MPEG
case TableID::PAT:
case TableID::CAT:
case TableID::PMT:
// ATSC
case TableID::MGT:
case TableID::TVCT:
case TableID::CVCT:
case TableID::RRT:
case TableID::EIT:
case TableID::ETT:
case TableID::STT:
case TableID::DET:
case TableID::DST:
has_sn = true;
break;
}
return has_sn;
}
bool PSIPTable::VerifyPSIP(bool verify_crc) const
{
if (verify_crc && (CalcCRC() != CRC()))
{
LOG(VB_SIPARSER, LOG_ERR,
QString("PSIPTable: Failed CRC check 0x%1 != 0x%2 "
"for StreamID = 0x%3")
.arg(CRC(),0,16).arg(CalcCRC(),0,16).arg(StreamID(),0,16));
return false;
}
unsigned char *bufend = _fullbuffer + _allocSize;
if ((_pesdata + 2) >= bufend)
return false; // can't query length
if (psipdata() >= bufend)
return false; // data outside buffer
if (TableID::PAT == TableID())
{
uint pcnt = (SectionLength() - PSIP_OFFSET - 2) >> 2;
bool ok = (psipdata() + (pcnt << 2) + 3 < bufend);
if (!ok)
{
LOG(VB_SIPARSER, LOG_ERR,
"PSIPTable: PAT: program list extends past end of buffer");
return false;
}
if ((Length() == 0xfff) && (TableIDExtension() == 0xffff) &&
(Section() == 0xff) && (LastSection() == 0xff))
{
LOG(VB_SIPARSER, LOG_ERR, "PSIPTable: PAT: All values at maximums");
return false;
}
return true;
}
if (TableID::PMT == TableID())
{
if (psipdata() + 3 >= bufend)
{
LOG(VB_SIPARSER, LOG_ERR,
"PSIPTable: PMT: can't query program info length");
return false;
}
if (psipdata() + Length() - 9 > bufend)
{
LOG(VB_SIPARSER, LOG_ERR,
"PSIPTable: PMT: reported length too large");
return false;
}
uint proginfolen = ((psipdata()[2]<<8) | psipdata()[3]) & 0x0fff;
const unsigned char *proginfo = psipdata() + 4;
const unsigned char *cpos = proginfo + proginfolen;
if (cpos > bufend)
{
LOG(VB_SIPARSER, LOG_ERR,
"PSIPTable: PMT: program info extends past end of buffer");
return false;
}
vector<unsigned char*> _ptrs;
const unsigned char *pos = cpos;
uint i = 0;
for (; pos < psipdata() + Length() - 9; i++)
{
const unsigned char *ptr = pos;
if (pos + 4 > bufend)
{
LOG(VB_SIPARSER, LOG_ERR,
QString("PSIPTable: PMT: stream info %1 extends "
"past end of buffer").arg(i));
return false;
}
pos += 5 + (((ptr[3] << 8) | ptr[4]) & 0x0fff);
}
if (pos > bufend)
{
LOG(VB_SIPARSER, LOG_ERR,
QString("PSIPTable: PMT: last stream info %1 extends "
"past end of buffer").arg(i));
return false;
}
return true;
}
return true;
}
ProgramAssociationTable* ProgramAssociationTable::CreateBlank(bool smallPacket)
{
(void) smallPacket; // currently always a small packet..
TSPacket *tspacket = TSPacket::CreatePayloadOnlyPacket();
memcpy(tspacket->data() + sizeof(TSHeader) + 1/* start of field pointer */,
DEFAULT_PAT_HEADER, sizeof(DEFAULT_PAT_HEADER));
PSIPTable psip = PSIPTable::View(*tspacket);
psip.SetLength(TSPacket::kPayloadSize
- 1 /* for start of field pointer */
- 3 /* for data before data last byte of pes length */);
ProgramAssociationTable *pat = new ProgramAssociationTable(psip);
pat->SetTotalLength(sizeof(DEFAULT_PAT_HEADER));
delete tspacket;
return pat;
}
ProgramAssociationTable* ProgramAssociationTable::Create(
uint tsid, uint version,
const vector<uint>& pnum, const vector<uint>& pid)
{
const uint count = min(pnum.size(), pid.size());
ProgramAssociationTable* pat = CreateBlank();
pat->SetVersionNumber(version);
pat->SetTranportStreamID(tsid);
pat->SetTotalLength(PSIP_OFFSET + (count * 4));
// create PAT data
if ((count * 4) >= (184 - (PSIP_OFFSET+1)))
{ // old PAT must be in single TS for this create function
LOG(VB_GENERAL, LOG_ERR,
"PAT::Create: Error, old PAT size exceeds maximum PAT size.");
delete pat;
return 0;
}
uint offset = PSIP_OFFSET;
for (uint i = 0; i < count; i++)
{
// pnum
pat->pesdata()[offset++] = pnum[i]>>8;
pat->pesdata()[offset++] = pnum[i] & 0xff;
// pid
pat->pesdata()[offset++] = ((pid[i]>>8) & 0x1f) | 0xe0;
pat->pesdata()[offset++] = pid[i] & 0xff;
}
pat->Finalize();
return pat;
}
ProgramMapTable* ProgramMapTable::CreateBlank(bool smallPacket)
{
ProgramMapTable *pmt = NULL;
TSPacket *tspacket = TSPacket::CreatePayloadOnlyPacket();
memcpy(tspacket->data() + sizeof(TSHeader) + 1/* start of field pointer */,
DEFAULT_PMT_HEADER, sizeof(DEFAULT_PMT_HEADER));
if (smallPacket)
{
PSIPTable psip = PSIPTable::View(*tspacket);
psip.SetLength(len_for_alloc[0]);
pmt = new ProgramMapTable(psip);
}
else
{
PSIPTable psip(*tspacket);
psip.SetLength(len_for_alloc[1]);
pmt = new ProgramMapTable(psip);
}
pmt->SetTotalLength(sizeof(DEFAULT_PMT_HEADER));
delete tspacket;
return pmt;
}
ProgramMapTable* ProgramMapTable::Create(
uint programNumber, uint basepid, uint pcrpid, uint version,
vector<uint> pids, vector<uint> types)
{
const uint count = min(pids.size(), types.size());
ProgramMapTable* pmt = CreateBlank(false);
pmt->tsheader()->SetPID(basepid);
pmt->RemoveAllStreams();
pmt->SetProgramNumber(programNumber);
pmt->SetPCRPID(pcrpid);
pmt->SetVersionNumber(version);
for (uint i=0; i<count; i++)
pmt->AppendStream(pids[i], types[i]);
pmt->Finalize();
return pmt;
}
ProgramMapTable* ProgramMapTable::Create(
uint programNumber, uint basepid, uint pcrpid, uint version,
const desc_list_t &global_desc,
const vector<uint> &pids,
const vector<uint> &types,
const vector<desc_list_t> &prog_desc)
{
const uint count = min(pids.size(), types.size());
ProgramMapTable* pmt = CreateBlank(false);
pmt->tsheader()->SetPID(basepid);
pmt->RemoveAllStreams();
pmt->SetProgramNumber(programNumber);
pmt->SetPCRPID(pcrpid);
pmt->SetVersionNumber(version);
vector<unsigned char> gdesc;
for (uint i=0; i<global_desc.size(); i++)
{
uint len = global_desc[i][1] + 2;
gdesc.insert(gdesc.end(), global_desc[i], global_desc[i] + len);
}
pmt->SetProgramInfo(&gdesc[0], gdesc.size());
for (uint i = 0; i < count; i++)
{
vector<unsigned char> pdesc;
for (uint j = 0; j < prog_desc[i].size(); j++)
{
uint len = prog_desc[i][j][1] + 2;
pdesc.insert(pdesc.end(),
prog_desc[i][j], prog_desc[i][j] + len);
}
pmt->AppendStream(pids[i], types[i], &pdesc[0], pdesc.size());
}
pmt->Finalize();
LOG(VB_SIPARSER, LOG_INFO, "Created PMT \n" + pmt->toString());
return pmt;
}
void ProgramMapTable::Parse() const
{
_ptrs.clear();
const unsigned char *cpos = psipdata() + pmt_header + ProgramInfoLength();
unsigned char *pos = const_cast<unsigned char*>(cpos);
for (uint i = 0; pos < psipdata() + Length() - 9; i++)
{
_ptrs.push_back(pos);
pos += 5 + StreamInfoLength(i);
#if 0
LOG(VB_SIPARSER, LOG_DEBUG, QString("Parsing PMT(0x%1) i(%2) len(%3)")
.arg((uint64_t)this, 0, 16) .arg(i) .arg(StreamInfoLength(i)));
#endif
}
_ptrs.push_back(pos);
#if 0
LOG(VB_SIPARSER, LOG_DEBUG, QString("Parsed PMT(0x%1)\n%2")
.arg((uint64_t)this, 0, 16) .arg(toString()));
#endif
}
void ProgramMapTable::AppendStream(
uint pid, uint type,
unsigned char* streamInfo, uint infoLength)
{
if (!StreamCount())
_ptrs.push_back(psipdata() + pmt_header + ProgramInfoLength());
memset(_ptrs[StreamCount()], 0xff, 5);
SetStreamPID(StreamCount(), pid);
SetStreamType(StreamCount(), type);
SetStreamProgramInfo(StreamCount(), streamInfo, infoLength);
_ptrs.push_back(_ptrs[StreamCount()]+5+StreamInfoLength(StreamCount()));
SetTotalLength(_ptrs[StreamCount()] - pesdata());
}
/** \fn ProgramMapTable::IsVideo(uint,QString) const
* \brief Returns true iff the stream at index i is a video stream.
*
* This of course returns true if StreamID::IsVideo() is true.
* And, it also returns true if IsVideo returns true after
* StreamID::Normalize() is used on the stream type.
*
* \param i index of stream
*/
bool ProgramMapTable::IsVideo(uint i, QString sistandard) const
{
if (StreamID::IsVideo(StreamType(i)))
return true;
desc_list_t list = MPEGDescriptor::
Parse(StreamInfo(i), StreamInfoLength(i));
uint stream_id = StreamID::Normalize(StreamType(i), list, sistandard);
return StreamID::IsVideo(stream_id);
}
/** \fn ProgramMapTable::IsAudio(uint,QString) const
* \brief Returns true iff the stream at index i is an audio stream.
*
* This of course returns true if StreamID::IsAudio() is true.
* And, it also returns true if IsAudio returns true after
* StreamID::Normalize() is used on the stream type.
*
* \param i index of stream
*/
bool ProgramMapTable::IsAudio(uint i, QString sistandard) const
{
if (StreamID::IsAudio(StreamType(i)))
return true;
desc_list_t list = MPEGDescriptor::
Parse(StreamInfo(i), StreamInfoLength(i));
uint stream_id = StreamID::Normalize(StreamType(i), list, sistandard);
return StreamID::IsAudio(stream_id);
}
/** \fn ProgramMapTable::IsEncrypted(QString sistandard) const
* \brief Returns true iff PMT contains CA descriptor for a vid/aud stream.
*/
bool ProgramMapTable::IsEncrypted(QString sistandard) const
{
bool encrypted = IsProgramEncrypted();
for (uint i = 0; !encrypted && i < StreamCount(); i++) {
/* Only check audio/video streams */
if (IsAudio(i,sistandard) || IsVideo(i,sistandard))
encrypted |= IsStreamEncrypted(i);
}
return encrypted;
}
/** \fn ProgramMapTable::IsProgramEncrypted(void) const
* \brief Returns true iff PMT's ProgramInfo contains CA descriptor.
*/
bool ProgramMapTable::IsProgramEncrypted(void) const
{
desc_list_t descs = MPEGDescriptor::ParseOnlyInclude(
ProgramInfo(), ProgramInfoLength(), DescriptorID::conditional_access);
uint encrypted = 0;
QMap<uint,uint> encryption_system;
for (uint i = 0; i < descs.size(); i++)
{
ConditionalAccessDescriptor cad(descs[i]);
encryption_system[cad.PID()] = cad.SystemID();
encrypted |= cad.SystemID();
#if 0
LOG(VB_GENERAL, LOG_INFO, "DTVsm: " + cad.toString());
#endif
}
return encrypted != 0;
}
/** \fn ProgramMapTable::IsStreamEncrypted(uint i) const
* \brief Returns true iff PMT contains CA descriptor.
*
* \param i index of stream
*/
bool ProgramMapTable::IsStreamEncrypted(uint i) const
{
desc_list_t descs = MPEGDescriptor::ParseOnlyInclude(
StreamInfo(i), StreamInfoLength(i), DescriptorID::conditional_access);
uint encrypted = 0;
QMap<uint,uint> encryption_system;
for (uint j = 0; j < descs.size(); j++)
{
ConditionalAccessDescriptor cad(descs[j]);
encryption_system[cad.PID()] = cad.SystemID();
encrypted |= cad.SystemID();
#if 0
LOG(VB_GENERAL, LOG_INFO, "DTVsm: " + cad.toString());
#endif
}
return encrypted != 0;
}
bool ProgramMapTable::IsStillPicture(QString sistandard) const
{
static const unsigned char STILL_PICTURE_FLAG = 0x01;
for (uint i = 0; i < StreamCount(); i++)
{
if (IsVideo(i, sistandard))
{
return StreamInfoLength(i) > 2 &&
(StreamInfo(i)[2] & STILL_PICTURE_FLAG);
}
}
return false;
}
/** \fn ProgramMapTable::FindPIDs(uint type, vector<uint>& pids,
const QString&) const
* \brief Finds all pids matching type.
* \param type StreamType to match
* \param pids vector pids will be added to
* \param sistandard standard to use in determining if this is a A/V stream
* \return number of pids in list
*/
uint ProgramMapTable::FindPIDs(uint type,
vector<uint> &pids,
const QString &sistandard) const
{
if ((StreamID::AnyMask & type) != StreamID::AnyMask)
{
for (uint i=0; i < StreamCount(); i++)
if (type == StreamType(i))
pids.push_back(StreamPID(i));
}
else if (StreamID::AnyVideo == type)
{
for (uint i=0; i < StreamCount(); i++)
if (IsVideo(i, sistandard))
pids.push_back(StreamPID(i));
}
else if (StreamID::AnyAudio == type)
{
for (uint i=0; i < StreamCount(); i++)
if (IsAudio(i, sistandard))
pids.push_back(StreamPID(i));
}
return pids.size();
}
/** \fn ProgramMapTable::FindPIDs(uint, vector<uint>&, vector<uint>&,
const QString&, bool normalize) const
* \brief Finds all pids w/types, matching type (useful for AnyVideo/AnyAudio).
* \param type StreamType to match
* \param pids vector pids will be added to
* \param types vector types will be added to
* \param sistandard standard to use in determining if this is a A/V stream
* \param normalize if set types will be normalized
* \return number of items in pids and types lists.
*/
uint ProgramMapTable::FindPIDs(uint type,
vector<uint> &pids,
vector<uint> &types,
const QString &sistandard,
bool normalize) const
{
uint pids_start = pids.size();
if ((StreamID::AnyMask & type) != StreamID::AnyMask)
{
for (uint i=0; i < StreamCount(); i++)
if (type == StreamType(i))
{
pids.push_back(StreamPID(i));
types.push_back(StreamType(i));
}
}
else if (StreamID::AnyVideo == type)
{
for (uint i=0; i < StreamCount(); i++)
if (IsVideo(i, sistandard))
{
pids.push_back(StreamPID(i));
types.push_back(StreamType(i));
}
}
else if (StreamID::AnyAudio == type)
{
for (uint i=0; i < StreamCount(); i++)
if (IsAudio(i, sistandard))
{
pids.push_back(StreamPID(i));
types.push_back(StreamType(i));
}
}
if (!normalize)
return pids.size();
for (uint i = pids_start; i < pids.size(); i++)
{
int index = FindPID(pids[i]);
if (index >= 0)
{
desc_list_t desc = MPEGDescriptor::Parse(
StreamInfo(i), StreamInfoLength(i));
types[i] = StreamID::Normalize(types[i], desc, sistandard);
}
}
return pids.size();
}
uint ProgramMapTable::FindUnusedPID(uint desired_pid)
{
uint pid = desired_pid;
while (FindPID(pid) >= 0)
pid += 0x10;
if (desired_pid <= 0x1fff)
return pid;
pid = desired_pid;
while (FindPID(desired_pid) >= 0)
desired_pid += 1;
if (desired_pid <= 0x1fff)
return pid;
pid = 0x20;
while (FindPID(desired_pid) >= 0)
desired_pid += 1;
return desired_pid & 0x1fff;
}
QString PSIPTable::toString(void) const
{
QString str;
str.append(QString(" PSIP tableID(0x%1) length(%2) extension(0x%3)\n")
.arg(TableID(), 0, 16).arg(Length())
.arg(TableIDExtension(), 0, 16));
str.append(QString(" version(%1) current(%2) "
"section(%3) last_section(%4)\n")
.arg(Version()).arg(IsCurrent())
.arg(Section()).arg(LastSection()));
if ((TableID() >= TableID::MGT) && (TableID() <= TableID::SRM))
{
str.append(QString(" atsc_protocol_version(%1)\n")
.arg(ATSCProtocolVersion()));
}
return str;
}
QString PSIPTable::toStringXML(uint indent_level) const
{
QString indent = xml_indent(indent_level);
return indent + "<PSIPSection " + XMLValues(indent_level + 1) + " />";
}
QString PSIPTable::XMLValues(uint indent_level) const
{
QString indent = xml_indent(indent_level);
QString str = QString(
"table_id=\"0x%1\" length=\"%2\"")
.arg(TableID(), 2, 16, QChar('0'))
.arg(Length());
if (HasSectionNumber())
{
str += QString(" section=\"%4\" last_section=\"%5\"")
.arg(Section()).arg(LastSection());
}
if ((TableID() >= TableID::MGT) && (TableID() <= TableID::SRM))
{
str += QString("\n%1version=\"%2\" current=\"%3\" "
"protocol_version=\"%4\" extension=\"0x%5\"")
.arg(indent)
.arg(Version()).arg(xml_bool_to_string(IsCurrent()))
.arg(ATSCProtocolVersion())
.arg(TableIDExtension(), 0, 16);
}
return str;
}
QString ProgramAssociationTable::toString(void) const
{
QString str;
str.append(QString("Program Association Section\n"));
str.append(PSIPTable::toString());
str.append(QString(" tsid(%1) ").arg(TransportStreamID()));
str.append(QString("programCount(%1)\n").arg(ProgramCount()));
uint cnt0 = 0, cnt1fff = 0;
for (uint i = 0; i < ProgramCount(); i++)
{
if (0x1fff == ProgramPID(i))
{
cnt1fff++;
continue;
}
if (0x0 == ProgramPID(i))
{
cnt0++;
continue;
}
str += QString(" program number %1 has PID 0x%2\n")
.arg(ProgramNumber(i),5)
.arg(ProgramPID(i),4,16,QChar('0'));
}
if (cnt0 || cnt1fff)
{
str.append(QString(" also contains %1 dummy programs\n")
.arg(cnt0 + cnt1fff));
}
return str;
}
QString ProgramAssociationTable::toStringXML(uint indent_level) const
{
QString indent_0 = xml_indent(indent_level);
QString indent_1 = xml_indent(indent_level + 1);
QString str = QString(
"%1<ProgramAssociationSection tsid=\"0x%2\" program_count=\"%3\""
"\n%4%5>\n")
.arg(indent_0)
.arg(TransportStreamID(),4,16,QChar('0'))
.arg(ProgramCount())
.arg(indent_1)
.arg(PSIPTable::XMLValues(indent_level + 1));
for (uint i = 0; i < ProgramCount(); i++)
{
bool dummy = (0x1fff == ProgramPID(i)) || (0x0 == ProgramPID(i));
str += QString("%1<Program number=\"%2\" pid=\"0x%3\" %4/>\n")
.arg(indent_1)
.arg(ProgramNumber(i))
.arg(ProgramPID(i),4,16,QChar('0'))
.arg(dummy ? "comment=\"Dummy Program\" " : "");
}
return str + indent_0 + "</ProgramAssociationSection>";
}
QString ProgramMapTable::toString(void) const
{
QString str =
QString("Program Map Section"
"\n%1"
" pnum(%2) pid(0x%3)\n")
.arg(PSIPTable::toString())
.arg(ProgramNumber())
.arg(tsheader()->PID(),0,16);
vector<const unsigned char*> desc =
MPEGDescriptor::Parse(ProgramInfo(), ProgramInfoLength());
for (uint i = 0; i < desc.size(); i++)
{
str.append(QString(" %1\n")
.arg(MPEGDescriptor(desc[i], 300).toString()));
}
for (uint i = 0; i < StreamCount(); i++)
{
str.append(QString(" Stream #%1 pid(0x%2) type(0x%3 %4)\n")
.arg(i).arg(StreamPID(i), 0, 16)
.arg(StreamType(i), 2, 16, QChar('0'))
.arg(StreamTypeString(i)));
vector<const unsigned char*> desc =
MPEGDescriptor::Parse(StreamInfo(i), StreamInfoLength(i));
for (uint i = 0; i < desc.size(); i++)
{
str.append(QString(" %1\n")
.arg(MPEGDescriptor(desc[i], 300).toString()));
}
}
return str;
}
QString ProgramMapTable::toStringXML(uint indent_level) const
{
QString indent_0 = xml_indent(indent_level);
QString indent_1 = xml_indent(indent_level + 1);
QString str = QString(
"%1<ProgramMapSection pcr_pid=\"0x%2\" program_number=\"%3\"\n"
"%4program_info_length=\"%5\" stream_count=\"%7\"%8>\n")
.arg(indent_0)
.arg(PCRPID(),0,16)
.arg(ProgramNumber())
.arg(indent_1)
.arg(ProgramInfoLength())
.arg(PSIPTable::XMLValues(indent_level + 1));
vector<const unsigned char*> gdesc =
MPEGDescriptor::Parse(ProgramInfo(), ProgramInfoLength());
for (uint i = 0; i < gdesc.size(); i++)
{
str += MPEGDescriptor(gdesc[i], 300)
.toStringXML(indent_level + 1) + "\n";
}
for (uint i = 0; i < StreamCount(); i++)
{
str += QString("%1<Stream pid=\"0x%2\" type=\"0x%3\" "
"type_desc=\"%4\" stream_info_length=\"%5\"")
.arg(indent_1)
.arg(StreamPID(i),2,16,QChar('0'))
.arg(StreamType(i),2,16,QChar('0'))
.arg(StreamTypeString(i))
.arg(StreamInfoLength(i));
vector<const unsigned char*> ldesc =
MPEGDescriptor::Parse(StreamInfo(i), StreamInfoLength(i));
str += (ldesc.empty()) ? " />\n" : ">\n";
for (uint i = 0; i < ldesc.size(); i++)
{
str += MPEGDescriptor(ldesc[i], 300)
.toStringXML(indent_level + 2) + "\n";
}
if (!ldesc.empty())
str += indent_1 + "</Stream>\n";
}
return str + indent_0 + "</ProgramMapSection>";
}
const char *StreamID::toString(uint streamID)
{
// valid for some ATSC/DVB stuff too
switch (streamID)
{
case StreamID::MPEG2Video:
return "video-mpeg2";
case StreamID::MPEG1Video:
return "video-mpeg1";
case StreamID::MPEG4Video:
return "video-mpeg4";
case StreamID::H264Video:
return "video-h264";
case StreamID::OpenCableVideo:
return "video-opencable";
// audio
case StreamID::AC3Audio:
return "audio-ac3"; // EIT, PMT
case StreamID::EAC3Audio:
return "audio-eac3"; // EIT, PMT
case StreamID::MPEG2Audio:
return "audio-mp2-layer[1,2,3]"; // EIT, PMT
case StreamID::MPEG1Audio:
return "audio-mp1-layer[1,2,3]"; // EIT, PMT
case StreamID::MPEG2AudioAmd1:
return "audio-aac-latm"; // EIT, PMT
case StreamID::MPEG2AACAudio:
return "audio-aac"; // EIT, PMT
case StreamID::DTSAudio:
return "audio-dts"; // EIT, PMT
// other
case StreamID::PrivSec:
return "private-sec";
case StreamID::PrivData:
return "private-data";
// DSMCC Object Carousel
case StreamID::DSMCC_A:
return "dsmcc-a encap";
case StreamID::DSMCC_B:
return "dsmcc-b std data";
case StreamID::DSMCC_C:
return "dsmcc-c NPD data";
case StreamID::DSMCC_D:
return "dsmcc-d data";
// Can be in any MPEG stream ATSC, DVB, or ARIB ; but defined in SCTE 35
case StreamID::Splice:
return "splice"; // PMT
//case TableID::STUFFING: XXX: Duplicate?
// return "stuffing"; // optionally in any
//case TableID::CENSOR: FIXME collides with StreamID::EAC3Audio
// return "censor"; // EIT, optionally in PMT
case TableID::ECN:
return "extended channel name";
case TableID::SRVLOC:
return "service location"; // required in VCT
case TableID::TSS: // other channels with same stuff
return "time-shifted service";
case TableID::CMPNAME: