-
Notifications
You must be signed in to change notification settings - Fork 15
/
MMT.h
4836 lines (4109 loc) · 145 KB
/
MMT.h
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
/*
MIT License
Copyright (c) 2021 Ravin.Wang(wangf1978@hotmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include "IP.h"
#include <stdlib.h>
/*
AIT Application Information Table
AMT Address Map Table
AL-FEC Application Layer Forward Error Correction
BIT Broadcaster Information Table
CA Conditional Access
CDT Common Data Table
CRI Clock Relation Information
CRID Content Reference Identifier
DCI Device Capability Information
ECM Entitlement Control Message
EIT Event Information Table
EMM Entitlement Management Control
GFD Generic File Delivery
HRBM Hypothetical Receiver Buffer Model
LCT Layout Configuration Table
LDT Linked Description Table
MFU Media Fragment Unit
MMT MPEG Media Transport
MMTP MMT Protocol
MPI Media Presentation Information
MPT MMT Package Table
MPU Media Processing Unit
NIT Network Information Table
PA Package Access
PLT Package List Table
SDT Service Description Table
SDTT Software Download Trigger
TLV Type Length Value
URL Uniform Resource Locator
*/
#include <stdint.h>
#include "Bitstream.h"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4200)
#pragma warning(disable:4201)
#pragma warning(disable:4101)
#pragma warning(disable:4100)
#pragma warning(disable:4189)
#pragma warning(disable:4127)
#pragma pack(push,1)
#define PACKED
#else
#define PACKED __attribute__ ((__packed__))
#endif
#define MAX_TLV_PKT_DATA_LENGTH UINT16_MAX
#define MAX_FULL_TLV_PACKET_LENGTH (MAX_TLV_PKT_DATA_LENGTH + 4)
#define MMT_FIX_HEADER_FMT_STR "%s%21s"
#define SERVICE_TYPE_MEANING(t) \
(t) == 0x00?"undefined":(\
(t) == 0x01?"Digital TV service":(\
(t) == 0x02?"Digital audio service":(\
(t) == 0xA1?"Special video service":(\
(t) == 0xA4?"Engineering service":(\
(t) == 0xC0?"Data service":(\
(t) == 0xC1?"Storage type service using TLV":(\
(t) == 0xC2?"Multimedia service":"undefined")))))))
#define MESSAGE_NAME_BY_PACKETID(pkt_id) (\
(pkt_id) == 0x0000?"PA Message":(\
(pkt_id) == 0x0001?"CA Message":(\
(pkt_id) == 0x8000?"MH-EIT":(\
(pkt_id) == 0x8001?"MH-AIT":(\
(pkt_id) == 0x8002?"MH-BIT":(\
(pkt_id) == 0x8003?"H-SDTT":(\
(pkt_id) == 0x8004?"MH-SDT":(\
(pkt_id) == 0x8005?"MH-TOT":(\
(pkt_id) == 0x8006?"MH-CDT":(\
(pkt_id) == 0x8007?"Data Message":(\
(pkt_id) == 0x8008?"MH-DIT":(\
(pkt_id) == 0x8009?"MH-SIT":"Message"))))))))))))
extern std::unordered_map<uint8_t, std::string> g_TLV_SI_descriptors;
extern void PrintDescriptor(int level, unsigned char* p);
namespace MMT
{
void PrintPacketIDAssignment();
void PrintMMTSITable();
void PrintMMTSIMessage();
void PrintMMTSIDescriptor();
enum TLV_PACKET_TYPE
{
TLV_Undefined_packet = 0x0,
TLV_IPv4_packet = 0x01,
TLV_IPv6_packet = 0x02,
TLV_Header_compressed_IP_packet = 0x03,
TLV_Transmission_control_signal_packet = 0xFE,
TLV_Null_packet = 0xFF
};
#define TLV_PACKET_TYPE_NAMEA(t) (\
(t) == TLV_IPv4_packet?"IPv4 packet":(\
(t) == TLV_IPv6_packet?"IPv6 packet":(\
(t) == TLV_Header_compressed_IP_packet?"Header compressed IP packet":(\
(t) == TLV_Transmission_control_signal_packet?"Transmission control signal packet":(\
(t) == TLV_Null_packet?"Null packet":"Undefined packet")))))
#define TLV_PACKET_TYEP_NAME TLV_PACKET_TYPE_NAMEA
enum ULE_PACKET_TYPE
{
ULE_Undefined_packet = 0x0,
ULE_Bridged_frame = 0x0001,
ULE_IPv4_packet = 0x0800,
ULE_Compressed_IP_packet_by_ROHC = 0x22F1,
ULE_Compressed_IP_packet_by_HCfB = 0x22F2,
ULE_IPv6_packet = 0x86DD
};
#define ULE_PACKET_TYPE_NAMEA(t) (\
(t) == ULE_Bridged_frame?"Bridged frame":(\
(t) == ULE_IPv4_packet?"IPv4 packet":(\
(t) == ULE_Compressed_IP_packet_by_ROHC?"Compressed IP packet by ROHC":(\
(t) == ULE_Compressed_IP_packet_by_HCfB?"Compressed IP packet by HCfB":(\
(t) == ULE_IPv6_packet?"IPv6 packet":"Undefined packet")))))
#define ULE_PACKET_TYPE_NAME ULE_PACKET_TYPE_NAMEA
struct TLVPacket
{
uint64_t start_bitpos = 0ULL;
uint8_t upper_2bits : 2;
uint8_t upper_6bits : 6;
uint8_t Packet_type;
uint16_t Data_length;
virtual ~TLVPacket(){}
virtual int Unpack(CBitstream& bs)
{
int nRet = RET_CODE_SUCCESS;
uint64_t left_bits = 0ULL;
start_bitpos = bs.Tell(&left_bits);
if (left_bits < (4ULL << 3))
return RET_CODE_BOX_TOO_SMALL;
upper_2bits = (uint8_t)bs.GetBits(2);
upper_6bits = (uint8_t)bs.GetBits(6);
Packet_type = bs.GetByte();
Data_length = bs.GetWord();
return nRet;
}
virtual uint32_t LeftBits(CBitstream& bs)
{
uint64_t cur_bitpos = bs.Tell();
if (cur_bitpos > start_bitpos)
{
uint64_t whole_packet_bits_size = ((uint64_t)Data_length + 4) << 3;
if (whole_packet_bits_size > cur_bitpos - start_bitpos)
{
uint64_t left_bits = whole_packet_bits_size - (cur_bitpos - start_bitpos);
assert(left_bits <= ((uint64_t)(MAX_FULL_TLV_PACKET_LENGTH) << 3));
return (uint32_t)left_bits;
}
}
return 0UL;
}
virtual void SkipLeftBits(CBitstream& bs)
{
uint64_t left_bits = LeftBits(bs);
if (left_bits > 0)
bs.SkipBits(left_bits);
}
virtual void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
fprintf(out, "-----------------------------------------------------------------------------------------\n");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "\n", szIndent, "file offset", start_bitpos >> 3);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": 0X%X\n", szIndent, "upper_2bits", upper_2bits);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": 0X%X\n", szIndent, "upper_6bits", upper_6bits);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %s\n", szIndent, "Packet_type", TLV_PACKET_TYEP_NAME(Packet_type));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %d\n", szIndent, "Data_length", Data_length);
}
}PACKED;
// Unidirectional Lightweight Encapsulation (ULE) for Transmission of IP Datagrams over an MPEG-2 Transport Stream(TS),
struct ULEPacket
{
uint64_t start_bitpos;
uint16_t Destination_flag : 1;
uint16_t Data_length : 15;
uint64_t Packet_type : 16;
uint64_t Destination_address : 48;
uint32_t CRC;
virtual int Unpack(CBitstream& bs)
{
int nRet = RET_CODE_SUCCESS;
uint64_t left_bits = 0ULL;
start_bitpos = bs.Tell(&left_bits);
if (left_bits < (10ULL << 3))
return RET_CODE_BOX_TOO_SMALL;
Destination_flag = (uint16_t)bs.GetBits(1);
Data_length = (uint8_t)bs.GetBits(15);
Packet_type = bs.GetWord();
if (Destination_flag == 0)
Destination_address = bs.GetBits(48);
return nRet;
}
virtual uint32_t LeftBits(CBitstream& bs)
{
uint64_t cur_bitpos = bs.Tell();
if (cur_bitpos > start_bitpos)
{
uint64_t whole_packet_bits_size = (((uint64_t)Data_length + 4)) << 3;
if (whole_packet_bits_size > cur_bitpos - start_bitpos)
{
uint64_t left_bits = whole_packet_bits_size - (cur_bitpos - start_bitpos);
assert(left_bits <= ((uint64_t)(MAX_FULL_TLV_PACKET_LENGTH) << 3));
return (uint32_t)left_bits;
}
}
return 0UL;
}
virtual void SkipLeftBits(CBitstream& bs)
{
uint64_t left_bits = LeftBits(bs);
if (left_bits > 0)
bs.SkipBits(left_bits);
}
virtual void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
fprintf(out, "-----------------------------------------------------------------------------------------\n");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": 0X%X\n", szIndent, "Destination_flag", Destination_flag);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": 0X%X\n", szIndent, "Data_length", Data_length);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %s\n", szIndent, "Packet_type", ULE_PACKET_TYPE_NAME(Packet_type));
if (Destination_flag)
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "\n", szIndent, "Destination_address", Destination_address);
}
}PACKED;
struct Undefined_TLVPacket : public TLVPacket
{
int Unpack(CBitstream& bs)
{
int iRet = TLVPacket::Unpack(bs);
if (iRet < 0)
return iRet;
SkipLeftBits(bs);
return iRet;
}
}PACKED;
using TLVNullPacket = Undefined_TLVPacket;
struct IPv4Packet : public TLVPacket
{
IP::V4::Header IPv4_Header;
IP::UDPHeader UDP_header;
};
struct IPv6Packet : public TLVPacket
{
IP::V6::Header IPv6_Header;
union
{
IP::UDPPacket* UDP_packet;
void* ptr_Data = nullptr;
};
virtual ~IPv6Packet() {
if (IPv6_Header.payload_type == IP::V6::PROTOCOL_UDP)
delete UDP_packet;
}
int Unpack(CBitstream& bs)
{
int iRet = TLVPacket::Unpack(bs);
if (iRet < 0)
return iRet;
if ((iRet = IPv6_Header.Unpack(bs)) < 0)
goto done;
if (IPv6_Header.payload_type == IP::V6::PROTOCOL_UDP)
{
UDP_packet = new IP::UDPPacket();
if ((iRet = UDP_packet->Unpack(bs)) < 0)
{
delete UDP_packet;
UDP_packet = nullptr;
goto done;
}
}
done:
SkipLeftBits(bs);
return iRet;
}
virtual void Print(FILE* fp = nullptr, int indent = 0)
{
TLVPacket::Print(fp, indent);
IPv6_Header.Print(fp, indent);
if (IPv6_Header.payload_type == IP::V6::PROTOCOL_UDP)
{
if (UDP_packet != nullptr)
UDP_packet->Print(fp, indent);
}
}
}PACKED;
extern const std::unordered_map<uint16_t, std::tuple<const char*, const char*>> MMT_SI_Descriptor_Descs;
struct MMTSIDescriptor
{
uint64_t start_bitpos = 0;
uint16_t descriptor_tag = 0;
uint8_t descriptor_length = 0;
virtual ~MMTSIDescriptor(){}
virtual int Unpack(CBitstream& bs)
{
int nRet = RET_CODE_SUCCESS;
uint64_t left_bits = 0ULL;
start_bitpos = bs.Tell(&left_bits);
if (left_bits < (3ULL << 3))
return RET_CODE_BOX_TOO_SMALL;
descriptor_tag = bs.GetWord();
descriptor_length = bs.GetByte();
return nRet;
}
virtual uint32_t LeftBits(CBitstream& bs)
{
uint64_t cur_bitpos = bs.Tell();
if (cur_bitpos > start_bitpos)
{
uint64_t whole_packet_bits_size = (((uint64_t)descriptor_length + 3)) << 3;
if (whole_packet_bits_size > cur_bitpos - start_bitpos)
{
uint64_t left_bits = whole_packet_bits_size - (cur_bitpos - start_bitpos);
assert(left_bits <= ((uint64_t)(UINT8_MAX + 3) << 3));
return (uint32_t)left_bits;
}
}
return 0UL;
}
virtual void SkipLeftBits(CBitstream& bs)
{
uint64_t left_bits = LeftBits(bs);
if (left_bits > 0)
bs.SkipBits(left_bits);
}
virtual void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
auto iter = MMT_SI_Descriptor_Descs.find(descriptor_tag);
fprintf(out, "%s++++++++++++++ Descriptor: %s +++++++++++++\n", szIndent, MMT_SI_Descriptor_Descs.cend() != iter?std::get<0>(iter->second):"");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "\n", szIndent, "file offset", start_bitpos >> 3);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": 0X%X\n", szIndent, "descriptor_tag", descriptor_tag);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %d\n", szIndent, "descriptor_length", descriptor_length);
}
}PACKED;
struct UnimplMMTSIDescriptor : public MMTSIDescriptor
{
int Unpack(CBitstream& bs)
{
int iRet = MMTSIDescriptor::Unpack(bs);
if (iRet < 0)
return iRet;
SkipLeftBits(bs);
return iRet;
}
}PACKED;
struct MHStreamIdentifierDescriptor : public MMTSIDescriptor
{
uint16_t component_tag = 0;
int Unpack(CBitstream& bs)
{
int iRet = MMTSIDescriptor::Unpack(bs);
if (iRet < 0)
return iRet;
uint64_t left_bits = 0;
bs.Tell(&left_bits);
if (left_bits < 16ULL)
return RET_CODE_BOX_TOO_SMALL;
component_tag = bs.GetWord();
return RET_CODE_SUCCESS;
}
void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
MMTSIDescriptor::Print(fp, indent);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "component_tag", component_tag, component_tag);
}
}PACKED;
struct VideoComponentDescriptor : public MMTSIDescriptor
{
enum VIDEO_RESOLUTION
{
VID_RES_UNSPECIFIED = 0,
VID_RES_180 = 1,
VID_RES_240,
VID_RES_480,
VID_RES_720,
VID_RES_1080,
VID_RES_2160,
VID_RES_4320,
};
uint64_t video_resolution : 4;
uint64_t video_aspect_ratio : 4;
uint64_t video_scan_flag : 1;
uint64_t reserved_0 : 2;
uint64_t video_frame_rate : 5;
uint64_t component_tag : 16;
uint64_t video_transfer_characteristics : 4;
uint64_t reserved_1 : 4;
uint64_t ISO_639_language_code : 24;
VideoComponentDescriptor()
: video_resolution(0)
, video_aspect_ratio(0)
, video_scan_flag(0)
, reserved_0(0)
, video_frame_rate(0)
, component_tag(0)
, video_transfer_characteristics(0)
, reserved_1(0)
, ISO_639_language_code(0) {
}
int Unpack(CBitstream& bs)
{
int iRet = MMTSIDescriptor::Unpack(bs);
if (iRet < 0)
return iRet;
uint64_t left_bits = 0;
bs.Tell(&left_bits);
if (left_bits < 64ULL)
return RET_CODE_BOX_TOO_SMALL;
video_resolution = bs.GetBits(4);
video_aspect_ratio = bs.GetBits(4);
video_scan_flag = bs.GetBits(1);
reserved_0 = bs.GetBits(2);
video_frame_rate = bs.GetBits(5);
component_tag = bs.GetBits(16);
video_transfer_characteristics = bs.GetBits(4);
reserved_1 = bs.GetBits(4);
ISO_639_language_code = bs.GetBits(24);
return iRet;
}
void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
MMTSIDescriptor::Print(fp, indent);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "(0X%" PRIX64 "), %s\n", szIndent, "video_resolution", (uint64_t)video_resolution, (uint64_t)video_resolution,
video_resolution == VID_RES_UNSPECIFIED?"Unspecified":(
video_resolution == VID_RES_180?"height: 180":(
video_resolution == VID_RES_240?"height: 240":(
video_resolution == VID_RES_480?"height: 480/525":(
video_resolution == VID_RES_720?"height: 720/750":(
video_resolution == VID_RES_1080?"height: 1080/1125":(
video_resolution == VID_RES_2160?"height: 2160":(
video_resolution == VID_RES_4320?"height: 4320":""))))))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "(0X%" PRIX64 "), %s\n", szIndent, "video_aspect_ratio", (uint64_t)video_aspect_ratio, (uint64_t)video_aspect_ratio,
video_aspect_ratio == 0?"Unspecified":(
video_aspect_ratio == 1?"4:3":(
video_aspect_ratio == 2?"16:9 with pan vector":(
video_aspect_ratio == 3?"16:9 without pan vector":(
video_aspect_ratio == 4?"> 16:9":"")))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 ", %s\n", szIndent, "video_scan_flag", (uint64_t)video_scan_flag, video_scan_flag ? "progressive" :"interlaced");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "(0X%" PRIX64 "), %s\n", szIndent, "video_frame_rate", (uint64_t)video_frame_rate, (uint64_t)video_frame_rate,
video_frame_rate == 0?"Unspecified":(
video_frame_rate == 1?"15 fps":(
video_frame_rate == 2?"24/1.001 fps":(
video_frame_rate == 3?"24 fps":(
video_frame_rate == 4?"25 fps":(
video_frame_rate == 5?"30/1.001 fps":(
video_frame_rate == 6?"30 fps":(
video_frame_rate == 7?"50 fps":(
video_frame_rate == 8?"60/1.001 fps":(
video_frame_rate == 9?"60 fps":(
video_frame_rate == 10?"100 fps":(
video_frame_rate == 11?"120/1.001 fps":(
video_frame_rate == 12?"120 fps":"")))))))))))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "(0X%" PRIX64 ")\n", szIndent, "component_tag", (uint64_t)component_tag, (uint64_t)component_tag);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu64 "(0X%" PRIX64 "), %s\n", szIndent, "trans_characteristics", (uint64_t)video_transfer_characteristics, (uint64_t)video_transfer_characteristics,
video_transfer_characteristics == 0?"Unspecified":(
video_transfer_characteristics == 1?"Rec. ITU-R BT.709-5":(
video_transfer_characteristics == 2?"IEC 61966-2-4":(
video_transfer_characteristics == 3?"Rec. ITU-R BT.2020":(
video_transfer_characteristics == 4?"Rec. ITU-R BT.2100 PQ":(
video_transfer_characteristics == 5?"Rec. ITU-R BT.2100 HLG" :""))))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %c%c%c (0X%08" PRIX32 ")\n", szIndent, "language",
isprint(((uint32_t)ISO_639_language_code >> 16) & 0xFF) ? ((uint32_t)ISO_639_language_code >> 16) & 0xFF : '.',
isprint(((uint32_t)ISO_639_language_code >> 8) & 0xFF) ? ((uint32_t)ISO_639_language_code >> 8) & 0xFF : '.',
isprint(((uint32_t)ISO_639_language_code) & 0xFF) ? ((uint32_t)ISO_639_language_code) & 0xFF : '.', (uint32_t)ISO_639_language_code);
}
}PACKED;
struct MPUTimestampDescriptor : public MMTSIDescriptor
{
std::vector<std::tuple<uint32_t, IP::NTPv4Data::NTPTimestampFormat>> MPU_sequence_present_timestamps;
int Unpack(CBitstream& bs)
{
int iRet = MMTSIDescriptor::Unpack(bs);
if (iRet < 0)
return iRet;
for (size_t i = 0; i < descriptor_length / 12UL; i++)
{
uint32_t seq_no = bs.GetDWord();
IP::NTPv4Data::NTPTimestampFormat present_timestamp;
present_timestamp.Seconds = bs.GetDWord();
present_timestamp.Fraction = bs.GetDWord();
MPU_sequence_present_timestamps.push_back(std::make_tuple(seq_no, present_timestamp));
}
SkipLeftBits(bs);
return iRet;
}
void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
MMTSIDescriptor::Print(fp, indent);
for (size_t i = 0; i < MPU_sequence_present_timestamps.size(); i++)
{
fprintf(out, MMT_FIX_HEADER_FMT_STR ": #%" PRIu32 "\n", szIndent, "MPU_timestamps", (uint32_t)i);
uint32_t seq_no = std::get<0>(MPU_sequence_present_timestamps[i]);
IP::NTPv4Data::NTPTimestampFormat& present_timestamp = std::get<1>(MPU_sequence_present_timestamps[i]);
fprintf(out, " " MMT_FIX_HEADER_FMT_STR ": %" PRIu32 "(0X%" PRIX32 ")\n", szIndent, "mpu_sequence_number", seq_no, seq_no);
fprintf(out, " " MMT_FIX_HEADER_FMT_STR ": " NTPTIME_FMT_STR "s, %s\n", szIndent, "mpu_presentation_time",
present_timestamp.GetValue(), present_timestamp.IsUnknown()?"":DateTimeStr(present_timestamp.Seconds, 1900, present_timestamp.Fraction).c_str());
}
}
};
struct MPUExtendedTimestampDescriptor : public MMTSIDescriptor
{
struct MPU_TIMESTAMP_OFFSETS
{
uint32_t mpu_sequence_number;
uint8_t mpu_presentation_time_leap_indicator : 2;
uint8_t reserved : 6;
uint16_t mpu_decoding_time_offset;
uint8_t num_of_au;
std::vector<std::tuple<uint16_t, uint16_t>>
offsets;
uint16_t GetLength(uint8_t pts_offset_type)
{
return 8 + num_of_au*(pts_offset_type == 2 ? 4 : 2);
}
int Unpack(CBitstream& bs, uint8_t pts_offset_type)
{
uint64_t left_bits = 0;
bs.Tell(&left_bits);
if (left_bits < 64ULL)
return RET_CODE_BOX_TOO_SMALL;
mpu_sequence_number = bs.GetDWord();
mpu_presentation_time_leap_indicator = (uint8_t)bs.GetBits(2);
reserved = (uint8_t)bs.GetBits(6);
mpu_decoding_time_offset = bs.GetWord();
num_of_au = bs.GetByte();
offsets.reserve(num_of_au);
for (size_t j = 0; j < num_of_au; j++)
{
uint16_t dts_pts_offset = bs.GetWord();
uint16_t pts_offset = pts_offset_type == 2 ? bs.GetWord() : 0;
offsets.push_back(std::make_tuple(dts_pts_offset, pts_offset));
}
return RET_CODE_SUCCESS;
}
void Print(FILE* fp = nullptr, int indent = 0, uint8_t pts_offset_type=0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu32 "(0X%" PRIX32 ")\n", szIndent, "mpu_sequence_number", mpu_sequence_number, mpu_sequence_number);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu32 "(0X%" PRIX32 ")\n", szIndent, "leap_indicator", mpu_presentation_time_leap_indicator, mpu_presentation_time_leap_indicator);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "mpu_decoding_time_offset", mpu_decoding_time_offset, mpu_decoding_time_offset);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "num_of_au", num_of_au, num_of_au);
for (size_t i = 0; i < offsets.size(); i++)
{
fprintf(out, MMT_FIX_HEADER_FMT_STR ": #%zu\n", szIndent, "MPU_timestamps", i);
uint16_t dts_pts_offset = std::get<0>(offsets[i]);
uint16_t pts_offset = std::get<1>(offsets[i]);
fprintf(out, " " MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "dts_pts_offset", dts_pts_offset, dts_pts_offset);
if (pts_offset_type == 2)
fprintf(out, " " MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "pts_offset", pts_offset, pts_offset);
}
}
};
uint8_t reserved : 5;
uint8_t pts_offset_type : 2;
uint8_t timescale_flag : 1;
uint32_t timescale = 0;
uint16_t default_pts_offset = 0;
std::list<MPU_TIMESTAMP_OFFSETS>
ts_offsets;
MPUExtendedTimestampDescriptor()
: reserved(0), pts_offset_type(0), timescale_flag(0) {
}
int Unpack(CBitstream& bs)
{
int iRet = MMTSIDescriptor::Unpack(bs);
if (iRet < 0)
return iRet;
uint64_t left_bits = 0;
bs.Tell(&left_bits);
if (left_bits < 8)
return RET_CODE_BOX_TOO_SMALL;
reserved = (uint8_t)bs.GetBits(5);
pts_offset_type = (uint8_t)bs.GetBits(2);
timescale_flag = (uint8_t)bs.GetBits(1);
left_bits -= 8;
if (timescale_flag)
{
if (left_bits < 32)
return RET_CODE_BOX_TOO_SMALL;
timescale = bs.GetDWord();
left_bits -= 32ULL;
}
if (pts_offset_type)
{
if (left_bits < 16)
return RET_CODE_BOX_TOO_SMALL;
default_pts_offset = bs.GetWord();
left_bits -= 16ULL;
}
while (left_bits > 64ULL)
{
ts_offsets.emplace_back();
auto& back = ts_offsets.back();
if ((iRet = back.Unpack(bs, pts_offset_type)) < 0)
break;
uint64_t used_bits = (uint64_t)back.GetLength(pts_offset_type) << 3;
if (left_bits < used_bits)
break;
left_bits -= used_bits;
}
SkipLeftBits(bs);
return iRet;
}
void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
MMTSIDescriptor::Print(fp, indent);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "pts_offset_type", pts_offset_type, pts_offset_type);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u\n", szIndent, "timescale_flag", timescale_flag);
if (timescale_flag)
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %" PRIu32 "(0X%" PRIX32 ")\n", szIndent, "timescale", timescale, timescale);
if (pts_offset_type)
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "default_pts_offset", default_pts_offset, default_pts_offset);
int i = 0;
for (auto& v : ts_offsets)
{
fprintf(out, MMT_FIX_HEADER_FMT_STR ": #%d\n", szIndent, "offsets", i);
v.Print(fp, indent + 4, pts_offset_type);
i++;
}
}
};
struct MHAudioComponentDescriptor : public MMTSIDescriptor
{
uint8_t reserved_for_future_use_0 : 4;
uint8_t stream_content : 4;
uint8_t Dialog_control:1;
uint8_t Sound_handicapped : 2;
uint8_t Audio_mode : 5;
uint16_t component_tag;
uint8_t stream_type;
uint8_t simulcast_group_tag;
uint32_t ES_multi_lingual_flag : 1;
uint32_t main_component_flag : 1;
uint32_t quality_indicator : 2;
uint32_t sampling_rate : 3;
uint32_t reserved_for_future_use_1 : 1;
uint32_t ISO_639_language_code : 24;
uint32_t ISO_639_language_code_2;
std::vector<uint8_t> text_chars;
static const char* GetAudioModeDesc(uint8_t amod)
{
switch (amod)
{
case 0: return "Reserved for use in the future";
case 1: return "1/0 mode (single mono)";
case 2: return "1/0+1/0 mode (dual mono)";
case 3: return "2/0 mode (stereo)";
case 4: return "2/1 mode";
case 5: return "3/0 mode";
case 6: return "2/2 mode";
case 7: return "3/1 mode";
case 8: return "3/2 mode";
case 9: return "3/2+LFE mode (3/2.1 mode)";
case 10: return "3/3.1 mode";
case 11: return "2/0/0-2/0/2-0.1 mode";
case 12: return "5/2.1 mode";
case 13: return "3/2/2.1 mode";
case 14: return "2/0/0-3/0/2-0.1 mode";
case 15: return "0/2/0-3/0/2-0.1 mode";
case 16: return "2/0/0-3/2/3-0.2 mode";
case 17: return "3/3/3-5/2/3-3/0/0.2 mode";
default: return "Reserved for use in the future";
}
}
int Unpack(CBitstream& bs)
{
int iRet = MMTSIDescriptor::Unpack(bs);
if (iRet < 0)
return iRet;
uint64_t left_bits = 0;
bs.Tell(&left_bits);
left_bits = AMP_MIN(left_bits, (uint64_t)descriptor_length << 3);
if (left_bits < (10ULL << 3))
return RET_CODE_BOX_TOO_SMALL;
reserved_for_future_use_0 = (uint8_t)bs.GetBits(4);
stream_content = (uint8_t)bs.GetBits(4);
Dialog_control = (uint8_t)bs.GetBits(1);
Sound_handicapped = (uint8_t)bs.GetBits(2);
Audio_mode = (uint8_t)bs.GetBits(5);
component_tag = bs.GetWord();
stream_type = bs.GetByte();
simulcast_group_tag = bs.GetByte();
ES_multi_lingual_flag = (uint32_t)bs.GetBits(1);
main_component_flag = (uint32_t)bs.GetBits(1);
quality_indicator = (uint32_t)bs.GetBits(2);
sampling_rate = (uint32_t)bs.GetBits(3);
reserved_for_future_use_1 = (uint32_t)bs.GetBits(1);
ISO_639_language_code = (uint32_t)bs.GetBits(24);
left_bits -= 10ULL << 3;
if (ES_multi_lingual_flag)
{
if (left_bits < 24)
return RET_CODE_BOX_TOO_SMALL;
ISO_639_language_code_2 = (uint32_t)bs.GetBits(24);
left_bits -= 24;
}
if (left_bits > 8 && (left_bits>>3) <= INT32_MAX)
{
text_chars.resize((int)(left_bits >> 3));
bs.Read(&text_chars[0], (int)(left_bits >> 3));
}
SkipLeftBits(bs);
return RET_CODE_SUCCESS;
}
void Print(FILE* fp = nullptr, int indent = 0)
{
FILE* out = fp ? fp : stdout;
char szIndent[84];
memset(szIndent, 0, _countof(szIndent));
if (indent > 0)
{
int ccIndent = AMP_MIN(indent, 80);
memset(szIndent, ' ', ccIndent);
}
MMTSIDescriptor::Print(fp, indent);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X), %s\n", szIndent, "stream_content", stream_content, stream_content,
stream_content == 0x0?"Reserved for use in the future":(
stream_content == 0x1?"Not used":(
stream_content == 0x2?"Sound stream that coding system is not specified":(
stream_content == 0x3?"Sound stream by MPEG-4 AAC":(
stream_content == 0x4?"Sound stream by MPEG-4 ALS":(
(stream_content == 0x5 || stream_content == 0x9)?"Not used":(
(stream_content >= 0x6 && stream_content <= 0x8)?"Reserved for use in the future":(
(stream_content == 0xA || stream_content == 0xB)?"Reserved for use in the future":"Defined by broadcasters"))))))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u, %s\n", szIndent, "Dialog_control", Dialog_control, Dialog_control?"Audio stream includes dialog control information":"Audio stream does not include dialog control information");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X), %s\n", szIndent, "Sound_handicapped", Sound_handicapped, Sound_handicapped,
Sound_handicapped == 0?"Audio for the handicapped is not specified":(
Sound_handicapped == 1?"Audio explanation for the visual handicapped persons":(
Sound_handicapped == 2?"Audio for the hearing impared persons":(
Sound_handicapped == 3?"Reserved for use in the future":""))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X), %s\n", szIndent, "Audio_mode", Audio_mode, Audio_mode, GetAudioModeDesc(Audio_mode));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "component_tag", component_tag, component_tag);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "stream_type", stream_type, stream_type);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u(0X%X)\n", szIndent, "simulcast_group_tag", simulcast_group_tag, simulcast_group_tag);
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u%s\n", szIndent, "ES_multi_lingual_flag", ES_multi_lingual_flag, ES_multi_lingual_flag?", two languages are multiplexed in ES":"");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u%s\n", szIndent, "main_component_flag", main_component_flag, main_component_flag ? ", main audio" : "");
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %u%s\n", szIndent, "quality_indicator", quality_indicator,
quality_indicator == 0 ? "Reserved for use in the future" : (
quality_indicator == 1 ? "Mode 1":(
quality_indicator == 2 ? "Mode 2":(
quality_indicator == 3 ? "Mode 3": "Unknown"))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %s\n", szIndent, "sampling_rate",
sampling_rate == 0 ? "Reserved for use in the future" : (
sampling_rate == 1 ? "16 kHZ":(
sampling_rate == 2 ? "22.05 kHZ":(
sampling_rate == 3 ? "24 kHZ": (
sampling_rate == 4 ? "Reserved": (
sampling_rate == 5 ? "32 kHZ": (
sampling_rate == 6 ? "44.1 kHZ": (
sampling_rate == 7 ? "48 kHZ": "Unknown"))))))));
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %c%c%c (0X%08X)\n", szIndent, "language",
isprint(((uint32_t)ISO_639_language_code >> 16) & 0xFF) ? ((uint32_t)ISO_639_language_code >> 16) & 0xFF : '.',
isprint(((uint32_t)ISO_639_language_code >> 8) & 0xFF) ? ((uint32_t)ISO_639_language_code >> 8) & 0xFF : '.',
isprint(((uint32_t)ISO_639_language_code) & 0xFF) ? ((uint32_t)ISO_639_language_code) & 0xFF : '.', (uint32_t)ISO_639_language_code);
if (ES_multi_lingual_flag)
{
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %c%c%c (0X%08X)\n", szIndent, "language2",
isprint(((uint32_t)ISO_639_language_code_2 >> 16) & 0xFF) ? ((uint32_t)ISO_639_language_code_2 >> 16) & 0xFF : '.',
isprint(((uint32_t)ISO_639_language_code_2 >> 8) & 0xFF) ? ((uint32_t)ISO_639_language_code_2 >> 8) & 0xFF : '.',
isprint(((uint32_t)ISO_639_language_code_2) & 0xFF) ? ((uint32_t)ISO_639_language_code_2) & 0xFF : '.', (uint32_t)ISO_639_language_code_2);
}
if (text_chars.size() > 0)
{
std::string text((const char*)&text_chars[0], text_chars.size());
fprintf(out, MMT_FIX_HEADER_FMT_STR ": %s\n", szIndent, "text", text.c_str());
}
}
};
struct Additional_Arib_Subtitle_Info
{