-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpts_parser.cpp
3209 lines (2739 loc) · 114 KB
/
mpts_parser.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
/*
Original code by Mike Cancilla (https://github.com/mikecancilla)
2019
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
/*
SCTE 35 in MPTS
http://www.scte.org/SCTEDocs/Standards/SCTE%2035%202016.pdf
*/
#include <cstdint>
#include <cstdio>
#include <cassert>
#include <cstdarg>
#include <vector>
#include <map>
#include <variant>
#include <memory>
#include "mpts_parser.h"
#include "mpts_descriptors.h"
#include "mpeg2_parser.h"
#include "avc_parser.h"
#define VIDEO_DATA_MEMORY_INCREMENT (500 * 1024)
#define SYNC_BYTE 0x47
//#define 36 - 63 n / a n / a ITU - T Rec.H.222.0 | ISO / IEC 13818 - 1 Reserved
//#define 64 - 255 n / a n / a User Private
mptsParser::mptsParser(size_t &filePosition)
: m_pVideoData(NULL)
, m_filePosition(filePosition)
, m_packetSize(0)
, m_programNumber(-1)
, m_programMapPid(-1)
, m_networkPid(0x0010)
, m_scte35Pid(-1)
, m_videoDataSize(0)
, m_videoBufferSize(0)
, m_bTerse(true)
, m_bAnalyzeElementaryStream(false)
, m_parser(nullptr)
{
}
mptsParser::~mptsParser()
{
popVideoData();
}
bool mptsParser::setTerse(bool tf)
{
bool ret = m_bTerse;
m_bTerse = tf;
return ret;
}
bool mptsParser::getTerse()
{
return m_bTerse;
}
bool mptsParser::setAnalyzeElementaryStream(bool tf)
{
bool ret = m_bAnalyzeElementaryStream;
m_bAnalyzeElementaryStream = tf;
return ret;
}
bool mptsParser::getAnalyzeElementaryStream()
{
return m_bAnalyzeElementaryStream;
}
void inline mptsParser::incPtr(uint8_t *&p, size_t bytes)
{
m_filePosition += util::incrementPtr(p, bytes);
}
// Table 2-34
// Initialize a map of ID to string type
void mptsParser::initStreamTypes(std::map <uint16_t, const char *> &streamMap)
{
streamMap[0x0] = "Reserved";
streamMap[0x1] = "MPEG-1 Video";
streamMap[0x2] = "MPEG-2 Video";
streamMap[0x3] = "MPEG-1 Audio";
streamMap[0x4] = "MPEG-2 Audio";
streamMap[0x5] = "ISO 13818-1 private sections";
streamMap[0x6] = "ISO 13818-1 PES private data";
streamMap[0x7] = "ISO 13522 MHEG";
streamMap[0x8] = "ISO 13818-1 DSM - CC";
streamMap[0x9] = "ISO 13818-1 auxiliary";
streamMap[0xa] = "ISO 13818-6 multi-protocol encap";
streamMap[0xb] = "ISO 13818-6 DSM-CC U-N msgs";
streamMap[0xc] = "ISO 13818-6 stream descriptors";
streamMap[0xd] = "ISO 13818-6 sections";
streamMap[0xe] = "ISO 13818-1 auxiliary";
streamMap[0xf] = "MPEG-2 AAC Audio";
streamMap[0x10] = "MPEG-4 Video";
streamMap[0x11] = "MPEG-4 LATM AAC Audio";
streamMap[0x12] = "MPEG-4 generic";
streamMap[0x13] = "ISO 14496-1 SL-packetized";
streamMap[0x14] = "ISO 13818-6 Synchronized Download Protocol";
streamMap[0x15] = "Metadata carried in PES packets";
streamMap[0x16] = "Metadata carried in metadata_sections";
streamMap[0x17] = "Metadata carried in ISO/IEC 13818-6 Data Carousel";
streamMap[0x18] = "Metadata carried in ISO/IEC 13818-6 Object Carousel";
streamMap[0x19] = "Metadata carried in ISO/IEC 13818-6 Synchronized Download Protocol";
streamMap[0x1a] = "IPMP stream (defined in ISO/IEC 13818-11, MPEG-2 IPMP)";
streamMap[0x1b] = "H.264 Video";
streamMap[0x1c] = "ISO/IEC 14496-3 Audio";
streamMap[0x1d] = "ISO/IEC 14496-17 Text";
streamMap[0x1e] = "Auxiliary video stream as defined in ISO/IEC 23002-3";
streamMap[0x1f] = "SVC video sub-bitstream of an AVC video stream";
streamMap[0x20] = "MVC video sub-bitstream of an AVC video stream";
streamMap[0x21] = "Video stream as defined in Rec. ITU-T T.800 | ISO/IEC 15444-1";
streamMap[0x22] = "Video stream for stereoscopic 3D services Rec. ITU-T H.262 | ISO/IEC 13818-2";
streamMap[0x23] = "Video stream for stereoscopic 3D services Rec. ITU-T H.264 | ISO/IEC 14496-10";
streamMap[0x24] = "HEVC video bitstream Rec. ITU-T H.265 | ISO/IEC 23008-2";
streamMap[0x25] = "HEVC video bitstream of profile in Annex A Rec. ITU-T H.265 | ISO/IEC 23008-2";
streamMap[0x26] = "AVC MVCD video sub-bitstream of profile defined in Annex I of Rec. ITU-T H.264 | ISO/IEC 14496-10";
streamMap[0x27] = "Timeline and External Media Information Stream";
streamMap[0x28] = "HEVC Annex G profile TemporalID0";
streamMap[0x29] = "HEVC Annex G profile";
streamMap[0x2A] = "HEVC Annex H profile TemporalID0";
streamMap[0x2B] = "HEVC Annex H profile";
streamMap[0x2C] = "Green access units carried in MPEG-2 sections";
streamMap[0x2D] = "ISO/IEC 23008-3 Audio with MHAS transport syntax – main stream";
streamMap[0x2E] = "ISO/IEC 23008-3 Audio with MHAS transport syntax – auxiliary stream";
streamMap[0x2F] = "Quality access units carried in sections";
streamMap[0x30] = "Media Orchestration Access Units carried in sections";
streamMap[0x31] = "HEVC Motion Constrained Tile Set, parameter sets, slice headers";
for (int i = 0x32; i < 0x7F; i++)
{
streamMap[i] = "ISO 13818-1 reserved";
}
streamMap[0x7F] = "IPMP Stream";
streamMap[0x80] = "DigiCipher II Video";
streamMap[0x81] = "A52 / AC-3 Audio";
streamMap[0x82] = "HDMV DTS Audio";
streamMap[0x83] = "LPCM Audio";
streamMap[0x84] = "SDDS Audio";
streamMap[0x85] = "ATSC Program ID";
streamMap[0x86] = "DTS-HD Audio";
streamMap[0x87] = "E-AC- 3 Audio";
streamMap[0x8a] = "DTS Audio";
streamMap[0x91] = "A52b / AC-3 Audio";
streamMap[0x92] = "DVD_SPU vls Subtitle";
streamMap[0x94] = "SDDS Audio";
streamMap[0xa0] = "MSCODEC Video";
streamMap[0xea] = "Private ES(VC-1)";
}
size_t mptsParser::pushVideoData(uint8_t *p, size_t size)
{
if(m_videoDataSize + size > m_videoBufferSize)
{
m_videoBufferSize += VIDEO_DATA_MEMORY_INCREMENT;
m_pVideoData = (uint8_t*) realloc((void*) m_pVideoData, m_videoBufferSize);
}
std::memcpy(m_pVideoData + m_videoDataSize, p, size);
m_videoDataSize += size;
return m_videoDataSize;
}
// Returns the amount of bytes left in the video buffer after compacting
size_t mptsParser::compactVideoData(size_t bytesToCompact)
{
size_t bytes_leftover = m_videoDataSize - bytesToCompact;
if(bytes_leftover > 0)
{
std::memcpy(m_pVideoData, m_pVideoData + bytesToCompact, bytes_leftover);
m_videoDataSize = bytes_leftover;
}
return bytes_leftover;
}
size_t mptsParser::getVideoDataSize()
{
return m_videoDataSize;
}
size_t mptsParser::popVideoData()
{
size_t ret = m_videoDataSize;
if(m_pVideoData)
{
free(m_pVideoData);
m_pVideoData = NULL;
}
m_videoDataSize = 0;
m_videoBufferSize = 0;
return ret;
}
size_t mptsParser::readPAT(uint8_t*& p, program_association_table& pat, bool payloadUnitStart)
{
uint8_t* p_start = p;
pat.payload_start_offset = 0;
pat.payload_unit_start = payloadUnitStart;
if (payloadUnitStart)
{
pat.payload_start_offset = *p; // Spec 2.4.4.1
incPtr(p, 1);
incPtr(p, pat.payload_start_offset);
}
pat.table_id = *p;
incPtr(p, 1);
pat.section_length = util::read2Bytes(p);
incPtr(p, 2);
pat.section_syntax_indicator = (0x8000 & pat.section_length) >> 15;
pat.section_length &= 0xFFF;
uint8_t* p_section_start = p;
pat.transport_stream_id = util::read2Bytes(p);
incPtr(p, 2);
pat.current_next_indicator = *p;
incPtr(p, 1);
pat.version_number = (pat.current_next_indicator & 0x3E) >> 1;
pat.current_next_indicator &= 0x1;
pat.section_number = *p;
incPtr(p, 1);
pat.last_section_number = *p;
incPtr(p, 1);
while ((p - p_section_start) < (pat.section_length - 4))
{
uint16_t program_number = util::read2Bytes(p);
incPtr(p, 2);
uint16_t pid = util::read2Bytes(p);
pid &= 0x1FFF;
incPtr(p, 2);
pat.program_numbers.emplace_back(program_number, pid);
}
return p - p_start;
}
// 2.4.4.3 Program association Table
//
// The Program Association Table provides the correspondence between a program_number and the PID value of the
// Transport Stream packets which carry the program definition.The program_number is the numeric label associated with
// a program.
size_t mptsParser::readPAT(uint8_t *&p, bool payloadUnitStart)
{
uint8_t* p_start = p;
uint8_t payload_start_offset = 0;
if(payloadUnitStart)
{
payload_start_offset = *p; // Spec 2.4.4.1
incPtr(p, 1);
incPtr(p, payload_start_offset);
}
uint8_t table_id = *p;
incPtr(p, 1);
uint16_t section_length = util::read2Bytes(p);
incPtr(p, 2);
uint8_t section_syntax_indicator = (0x8000 & section_length) >> 15;
section_length &= 0xFFF;
uint8_t *p_section_start = p;
uint16_t transport_stream_id = util::read2Bytes(p);
incPtr(p, 2);
uint8_t current_next_indicator = *p;
incPtr(p, 1);
uint8_t version_number = (current_next_indicator & 0x3E) >> 1;
current_next_indicator &= 0x1;
uint8_t section_number = *p;
incPtr(p, 1);
uint8_t last_section_number = *p;
incPtr(p, 1);
printfXml(2, "<program_association_table>\n");
if(payloadUnitStart)
printfXml(3, "<pointer_field>0x%x</pointer_field>\n", payload_start_offset);
printfXml(3, "<table_id>0x%x</table_id>\n", table_id);
printfXml(3, "<section_syntax_indicator>%d</section_syntax_indicator>\n", section_syntax_indicator);
printfXml(3, "<section_length>%d</section_length>\n", section_length);
printfXml(3, "<transport_stream_id>0x%x</transport_stream_id>\n", transport_stream_id);
printfXml(3, "<version_number>0x%x</version_number>\n", version_number);
printfXml(3, "<current_next_indicator>0x%x</current_next_indicator>\n", current_next_indicator);
printfXml(3, "<section_number>0x%x</section_number>\n", section_number);
printfXml(3, "<last_section_number>0x%x</last_section_number>\n", last_section_number);
while ((p - p_section_start) < (section_length - 4))
{
m_programNumber = util::read2Bytes(p);
incPtr(p, 2);
uint16_t network_pid = 0;
if (0 == m_programNumber)
{
network_pid = util::read2Bytes(p);
incPtr(p, 2);
network_pid &= 0x1FFF;
m_networkPid = network_pid;
}
else
{
m_programMapPid = util::read2Bytes(p);
incPtr(p, 2);
m_programMapPid &= 0x1FFF;
}
printfXml(3, "<program>\n");
printfXml(4, "<number>%d</number>\n", m_programNumber);
if(network_pid)
printfXml(4, "<network_pid>0x%x</network_pid>\n", m_networkPid);
else
printfXml(4, "<program_map_pid>0x%x</program_map_pid>\n", m_programMapPid);
printfXml(3, "</program>\n");
}
printfXml(2, "</program_association_table>\n");
return p - p_start;
}
// 2.4.4.9 Program Map Table
//
// The Program Map Table provides the mappings between program numbers and the program elements that comprise
// them. A single instance of such a mapping is referred to as a "program definition". The program map table is the
// complete collection of all program definitions for a Transport Stream.
size_t mptsParser::readPMT(uint8_t*& p, program_map_table& pmt, bool payloadUnitStart)
{
uint8_t* p_start = p;
pmt.payload_start_offset = 0;
pmt.payload_unit_start = payloadUnitStart;
if (payloadUnitStart)
{
pmt.payload_start_offset = *p; // Spec 2.4.4.1
incPtr(p, 1);
incPtr(p, pmt.payload_start_offset);
}
pmt.table_id = *p;
incPtr(p, 1);
pmt.section_length = util::read2Bytes(p);
incPtr(p, 2);
pmt.section_syntax_indicator = pmt.section_length & 0x80 >> 15;
pmt.section_length &= 0xFFF;
uint8_t* p_section_start = p;
pmt.program_number = util::read2Bytes(p);
incPtr(p, 2);
pmt.current_next_indicator = *p;
incPtr(p, 1);
pmt.version_number = (pmt.current_next_indicator & 0x3E) >> 1;
pmt.current_next_indicator &= 0x1;
pmt.section_number = *p;
incPtr(p, 1);
pmt.last_section_number = *p;
incPtr(p, 1);
pmt.pcr_pid = util::read2Bytes(p);
incPtr(p, 2);
pmt.pcr_pid &= 0x1FFF;
pmt.program_info_length = util::read2Bytes(p);
incPtr(p, 2);
pmt.program_info_length &= 0x3FF;
p += readElementDescriptors(p, pmt);
// Subtract 4 from section_length to account for 4 byte CRC at its end. The CRC is not program data.
while ((p - p_section_start) < (pmt.section_length - 4))
{
uint8_t stream_type = *p;
incPtr(p, 1);
uint16_t elementary_pid = util::read2Bytes(p);
incPtr(p, 2);
elementary_pid &= 0x1FFF;
uint16_t es_info_length = util::read2Bytes(p);
incPtr(p, 2);
es_info_length &= 0xFFF;
p += es_info_length;
pmt.program_elements.emplace_back(stream_type, elementary_pid, es_info_length);
}
return p - p_start;
}
// 2.4.4.9 Program Map Table
//
// The Program Map Table provides the mappings between program numbers and the program elements that comprise
// them. A single instance of such a mapping is referred to as a "program definition". The program map table is the
// complete collection of all program definitions for a Transport Stream.
size_t mptsParser::readPMT(uint8_t *&p, bool payloadUnitStart)
{
uint8_t* p_start = p;
uint8_t payload_start_offset = 0;
if(payloadUnitStart)
{
payload_start_offset = *p; // Spec 2.4.4.1
incPtr(p, 1);
incPtr(p, payload_start_offset);
}
uint8_t table_id = *p;
incPtr(p, 1);
uint16_t section_length = util::read2Bytes(p);
incPtr(p, 2);
uint8_t section_syntax_indicator = section_length & 0x80 >> 15;
section_length &= 0xFFF;
uint8_t *p_section_start = p;
uint16_t program_number = util::read2Bytes(p);
incPtr(p, 2);
uint8_t current_next_indicator = *p;
incPtr(p, 1);
uint8_t version_number = (current_next_indicator & 0x3E) >> 1;
current_next_indicator &= 0x1;
uint8_t section_number = *p;
incPtr(p, 1);
uint8_t last_section_number = *p;
incPtr(p, 1);
uint16_t pcr_pid = util::read2Bytes(p);
incPtr(p, 2);
pcr_pid &= 0x1FFF;
m_pidToNameMap[pcr_pid] = "PCR";
uint16_t program_info_length = util::read2Bytes(p);
incPtr(p, 2);
program_info_length &= 0x3FF;
printfXml(2, "<program_map_table>\n");
if(payloadUnitStart)
printfXml(3, "<pointer_field>0x%x</pointer_field>\n", payload_start_offset);
printfXml(3, "<table_id>0x%x</table_id>\n", table_id);
printfXml(3, "<section_syntax_indicator>%d</section_syntax_indicator>\n", section_syntax_indicator);
printfXml(3, "<section_length>%d</section_length>\n", section_length);
printfXml(3, "<program_number>%d</program_number>\n", program_number);
printfXml(3, "<version_number>%d</version_number>\n", version_number);
printfXml(3, "<current_next_indicator>%d</current_next_indicator>\n", current_next_indicator);
printfXml(3, "<section_number>%d</section_number>\n", section_number);
printfXml(3, "<last_section_number>%d</last_section_number>\n", last_section_number);
printfXml(3, "<pcr_pid>0x%x</pcr_pid>\n", pcr_pid);
printfXml(3, "<program_info_length>%d</program_info_length>\n", program_info_length);
p += readElementDescriptors(p, program_info_length);
//my_printf("program_number:%d, pcr_pid:%x\n", program_number, pcr_pid);
//my_printf(" Elementary Streams:\n");
std::map <uint16_t, const char *> streamMap; // ID, name
initStreamTypes(streamMap);
// This has to be done by hand
m_pidToNameMap[0x1FFF] = "NULL Packet";
size_t stream_count = 0;
// Subtract 4 from section_length to account for 4 byte CRC at its end. The CRC is not program data.
while((p - p_section_start) < (section_length - 4))
{
uint8_t stream_type = *p;
incPtr(p, 1);
uint16_t elementary_pid = util::read2Bytes(p);
incPtr(p, 2);
elementary_pid &= 0x1FFF;
uint16_t es_info_length = util::read2Bytes(p);
incPtr(p, 2);
es_info_length &= 0xFFF;
p += es_info_length;
// Scte35 stream type is 0x86
if(0x86 == stream_type)
m_scte35Pid = elementary_pid;
m_pidToNameMap[elementary_pid] = streamMap[stream_type];
m_pidToTypeMap[elementary_pid] = (eMptsStreamType) stream_type;
//my_printf(" %d) pid:%x, stream_type:%x (%s)\n", stream_count++, elementary_pid, stream_type, streamMap[stream_type]);
printfXml(3, "<stream>\n");
printfXml(4, "<number>%zd</number>\n", stream_count);
printfXml(4, "<pid>0x%x</pid>\n", elementary_pid);
printfXml(4, "<type_number>0x%x</type_number>\n", stream_type);
printfXml(4, "<type_name>%s</type_name>\n", streamMap[stream_type]);
printfXml(3, "</stream>\n");
stream_count++;
}
printfXml(2, "</program_map_table>\n");
return p - p_start;
}
void mptsParser::printElementDescriptors(const program_map_table& pmt)
{
unsigned int descriptorNumber = 0;
for (const auto ped : pmt.program_element_descriptors)
{
printfXml(3, "<descriptor>\n");
printfXml(4, "<number>%d</number>\n", descriptorNumber++);
printfXml(4, "<tag>%d</tag>\n", ped.descriptor_tag);
printfXml(4, "<length>%d</length>\n", ped.descriptor_length);
if (auto value = std::get_if<std::shared_ptr<video_stream_descriptor>>(ped.descriptor.get()))
{
auto pVd = value->get();
printfXml(4, "<type>video_stream_descriptor</type>\n");
printfXml(4, "<multiple_frame_rate_flag>%d</multiple_frame_rate_flag>\n", pVd->multiple_frame_rate_flag);
printfXml(4, "<frame_rate_code>0x%x</frame_rate_code>\n", pVd->frame_rate_code);
printfXml(4, "<mpeg_1_only_flag>%d</mpeg_1_only_flag>\n", pVd->mpeg_1_only_flag);
printfXml(4, "<constrained_parameter_flag>%d</constrained_parameter_flag>\n", pVd->constrained_parameter_flag);
printfXml(4, "<still_picture_flag>%d</still_picture_flag>\n", pVd->still_picture_flag);
if (!pVd->mpeg_1_only_flag)
{
printfXml(4, "<profile_and_level_indication>0x%x</profile_and_level_indication>\n", pVd->profile_and_level_indication);
printfXml(4, "<chroma_format>%d</chroma_format>\n", pVd->chroma_format);
printfXml(4, "<frame_rate_extension_flag>%d</frame_rate_extension_flag>\n", pVd->frame_rate_extension_flag);
}
}
else if (auto value = std::get_if<std::shared_ptr<audio_stream_descriptor>>(ped.descriptor.get()))
{
auto pAd = value->get();
printfXml(4, "<type>audio_stream_descriptor</type>\n");
printfXml(4, "<free_format_flag>%d</free_format_flag>\n", pAd->free_format_flag);
printfXml(4, "<id>%d</id>\n", pAd->id);
printfXml(4, "<layer>%d</layer>\n", pAd->layer);
printfXml(4, "<variable_rate_audio_indicator>%d</variable_rate_audio_indicator>\n", pAd->variable_rate_audio_indicator);
}
else if (auto value = std::get_if<std::shared_ptr<registration_descriptor>>(ped.descriptor.get()))
{
auto pRd = value->get();
char sz_temp[5];
char* pChar = (char*)&(pRd->format_identifier);
sz_temp[3] = *pChar++;
sz_temp[2] = *pChar++;
sz_temp[1] = *pChar++;
sz_temp[0] = *pChar;
sz_temp[4] = 0;
printfXml(4, "<type>registration_descriptor</type>\n");
printfXml(4, "<format_identifier>%s</format_identifier>\n", sz_temp);
}
printfXml(3, "</descriptor>\n");
}
}
// 2.6 Program and program element descriptors
// Program and program element descriptors are structures which may be used to extend the definitions of programs and
// program elements.All descriptors have a format which begins with an 8 - bit tag value.The tag value is followed by an
// 8 - bit descriptor length and data fields.
size_t mptsParser::readElementDescriptors(uint8_t* p, program_map_table& pmt)
{
uint8_t* p_start = p;
uint32_t descriptor_number = 0;
uint32_t scte35_format_identifier = 0;
// PUSH BACK program_element_descriptors here
program_element_descriptor ped;
while (p - p_start < pmt.program_info_length)
{
ped.reset();
ped.descriptor_tag = *p;
incPtr(p, 1);
ped.descriptor_length = *p;
incPtr(p, 1);
//printfXml(3, "<descriptor>\n");
//printfXml(4, "<number>%d</number>\n", descriptor_number);
//printfXml(4, "<tag>%d</tag>\n", ped.descriptor_tag);
//printfXml(4, "<length>%d</length>\n", ped.descriptor_length);
switch (ped.descriptor_tag)
{
case VIDEO_STREAM_DESCRIPTOR:
{
/*
video_stream_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
multiple_frame_rate_flag 1 bslbf
frame_rate_code 4 uimsbf
mpeg_1_only_flag 1 bslbf
constrained_parameter_flag 1 bslbf
still_picture_flag 1 bslbf
if (MPEG_1_only_flag = = '0') {
profile_and_level_indication 8 uimsbf
chroma_format 2 uimsbf
frame_rate_extension_flag 1 bslbf
Reserved 5 bslbf
}
}
*/
std::shared_ptr<video_stream_descriptor> pVd = std::make_shared<video_stream_descriptor>();
pVd->multiple_frame_rate_flag = *p;
incPtr(p, 1);
pVd->frame_rate_code = (pVd->multiple_frame_rate_flag & 0x78) >> 3;
pVd->mpeg_1_only_flag = (pVd->multiple_frame_rate_flag & 0x04) >> 2;
pVd->constrained_parameter_flag = (pVd->multiple_frame_rate_flag & 0x02) >> 1;
pVd->still_picture_flag = (pVd->multiple_frame_rate_flag & 0x01);
pVd->multiple_frame_rate_flag >>= 7;
if (!pVd->mpeg_1_only_flag)
{
pVd->profile_and_level_indication = *p;
incPtr(p, 1);
pVd->chroma_format = *p;
incPtr(p, 1);
pVd->frame_rate_extension_flag = (pVd->chroma_format & 0x10) >> 4;
pVd->chroma_format >>= 6;
}
*ped.descriptor = std::move(pVd);
}
break;
case AUDIO_STREAM_DESCRIPTOR:
{
/*
audio_stream_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
free_format_flag 1 bslbf
ID 1 bslbf
layer 2 bslbf
variable_rate_audio_indicator 1 bslbf
reserved 3 bslbf
}
*/
std::shared_ptr<audio_stream_descriptor> pAd = std::make_shared<audio_stream_descriptor>();
pAd->free_format_flag = *p;
incPtr(p, 1);
pAd->id = (pAd->free_format_flag & 0x40) >> 6;
pAd->layer = (pAd->free_format_flag & 0x30) >> 4;
pAd->variable_rate_audio_indicator = (pAd->free_format_flag & 0x08) >> 3;
pAd->free_format_flag >>= 7;
*ped.descriptor = std::move(pAd);
}
break;
case HIERARCHY_DESCRIPTOR:
{
/*
hierarchy_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
reserved 4 bslbf
hierarchy_type 4 uimsbf
reserved 2 bslbf
hierarchy_layer_index 6 uimsbf
reserved 2 bslbf
hierarchy_embedded_layer_index 6 uimsbf
reserved 2 bslbf
hierarchy_channel 6 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>hierarchy_descriptor</type>\n");
}
break;
case REGISTRATION_DESCRIPTOR:
{
/*
registration_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
format_identifier 32 uimsbf
for (i = 0; i < N; i++){
additional_identification_info 8 bslbf
}
}
*/
std::shared_ptr<registration_descriptor> pRd = std::make_shared<registration_descriptor>();
pRd->format_identifier = util::read4Bytes(p);
incPtr(p, 4);
// if (0x43554549 == pRd->format_identifier)
// scte35_format_identifier = format_identifier; // Should be 0x43554549 (ASCII CUEI)
incPtr(p, ped.descriptor_length - 4);
ped.descriptor = std::make_shared<mpts_descriptor>();
*ped.descriptor = std::move(pRd);
}
break;
case DATA_STREAM_ALIGNMENT_DESCRIPTOR:
{
/*
data_stream_alignment_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
alignment_type 8 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>data_stream_alignment_descriptor</type>\n");
}
break;
case TARGET_BACKGROUND_GRID_DESCRIPTOR:
{
/*
target_background_grid_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
horizontal_size 14 uimsbf
vertical_size 14 uimsbf
aspect_ratio_information 4 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>target_background_grid_descriptor</type>\n");
}
break;
case VIDEO_WINDOW_DESCRIPTOR:
{
/*
video_window_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
horizontal_offset 14 uimsbf
vertical_offset 14 uimsbf
window_priority 4 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>video_window_descriptor</type>\n");
}
break;
case CA_DESCRIPTOR:
{
/*
CA_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
CA_system_ID 16 uimsbf
reserved 3 bslbf
CA_PID 13 uimsbf
for (i = 0; i < N; i++) {
private_data_byte 8 uimsbf
}
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>ca_descriptor</type>\n");
}
break;
case ISO_639_LANGUAGE_DESCRIPTOR:
{
/*
ISO_639_language_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
for (i = 0; i < N; i++) {
ISO_639_language_code 24 bslbf
audio_type 8 bslbf
}
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>iso_639_language_descriptor</type>\n");
}
break;
case SYSTEM_CLOCK_DESCRIPTOR:
{
/*
system_clock_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
external_clock_reference_indicator 1 bslbf
reserved 1 bslbf
clock_accuracy_integer 6 uimsbf
clock_accuracy_exponent 3 uimsbf
reserved 5 bslbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>system_clock_descriptor</type>\n");
}
break;
case MULTIPLEX_BUFFER_UTILIZATION_DESCRIPTOR:
{
/*
Multiplex_buffer_utilization_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
bound_valid_flag 1 bslbf
LTW_offset_lower_bound 15 uimsbf
reserved 1 bslbf
LTW_offset_upper_bound 15 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>multiplex_buffer_utilization_descriptor</type>\n");
}
break;
case COPYRIGHT_DESCRIPTOR:
{
/*
copyright_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
copyright_identifier 32 uimsbf
for (i = 0; i < N; i++){
additional_copyright_info 8 bslbf
}
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>copyright_descriptor</type>\n");
}
break;
case MAXIMUM_BITRATE_DESCRIPTOR:
{
/*
maximum_bitrate_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
reserved 2 bslbf
maximum_bitrate 22 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>maximum_bitrate_descriptor</type>\n");
}
break;
case PRIVATE_DATA_INDICATOR_DESCRIPTOR:
{
/*
private_data_indicator_descriptor() {
descriptor_tag 38 uimsbf
descriptor_length 38 uimsbf
private_data_indicator 32 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>private_data_indicator_descriptor</type>\n");
}
break;
case SMOOTHING_BUFFER_DESCRIPTOR:
{
/*
smoothing_buffer_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
reserved 2 bslbf
sb_leak_rate 22 uimsbf
reserved 2 bslbf
sb_size 22 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>smoothing_buffer_descriptor</type>\n");
}
break;
case STD_DESCRIPTOR:
{
/*
STD_descriptor () {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
reserved 7 bslbf
leak_valid_flag 1 bslbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>std_descriptor</type>\n");
}
case IBP_DESCRIPTOR:
{
/*
ibp_descriptor() {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
closed_gop_flag 1 uimsbf
identical_gop_flag 1 uimsbf
max_gop-length 14 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>ibp_descriptor</type>\n");
}
break;
case MPEG_4_VIDEO_DESCRIPTOR:
{
/*
MPEG-4_video_descriptor () {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
MPEG-4_visual_profile_and_level 8 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>mpeg_4_video_descriptor</type>\n");
}
break;
case MPEG_4_AUDIO_DESCRIPTOR:
{
/*
MPEG-4_audio_descriptor () {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
MPEG-4_audio_profile_and_level 8 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>mpeg_4_audio_descriptor</type>\n");
}
break;
case IOD_DESCRIPTOR:
{
/*
IOD_descriptor () {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
Scope_of_IOD_label 8 uimsbf
IOD_label 8 uimsbf
InitialObjectDescriptor () 8 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>iod_descriptor</type>\n");
}
break;
case SL_DESCRIPTOR:
{
/*
SL_descriptor () {
descriptor_tag 8 uimsbf
descriptor_length 8 uimsbf
ES_ID 16 uimsbf
}
*/
incPtr(p, ped.descriptor_length); // TODO
printfXml(4, "<type>sl_descriptor</type>\n");
}
break;