-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.pl
executable file
·3224 lines (2937 loc) · 125 KB
/
ts.pl
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
#!/usr/bin/perl
##########################################################################
# TS Parser
# Copyright (C) 2012 Tomonobu Saito All Rights Reserved.
use strict;
use warnings;
use TSPacket;
use PacketBuffer;
use DuplicateChecker;
use PolsatPIS;
#====================================================================
# CONFIGRATION { BEGIN }
#====================================================================
my(@gen_pid_list);
push(@gen_pid_list, 0x00); # PAT
push(@gen_pid_list, 0x10); # NIT
push(@gen_pid_list, 0x11); # SDT, BAT
push(@gen_pid_list, 0x12); # EIT
push(@gen_pid_list, 0x14); # TOT/TDT
my $check_version = 1; # 0 ... no check, 1 ... check, 2 ... with log
my $check_crc32 = 1; # 0 ... no check, 1 ... check, 2 ... with log
my $check_integrity = 1; # 0 ... no check, 1 ... check
my $dump_summary = 1; # 0 ... not show, 1 ... show
my $dump_descriptor = 1; # 0 ... not show, 1 ... show
my $dump_err_packet = 1; # 0 ... not dump, 1 ... dump
my $dump_polsat_epg = 1; # 0 ... not dump, 1 ... dump, 2 ... dump file
#====================================================================
# CONFIGRATION { END }
#====================================================================
#====================================================================
# GLOBAL VALUE { BEGIN }
#====================================================================
my $polsat_epg_pid = undef;
my @polsat_pis_list;
#====================================================================
# GLOBAL VALUE { END }
#====================================================================
# global const.
use constant numeric =>
[ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' ];
unless (1 <= $#ARGV) {
print "*** USAGE ***\n";
print "ts.pl <TS FILE> <0 | NUMBER OF PACKTES TO PARSE>\n";
exit(0);
}
main_loop($ARGV[0], $ARGV[1]);
exit(0);
##########################################################################
#
# main_loop
#
sub main_loop {
my($file, $count) = @_;
# open the target file.
my $file_size = 0;
unless (open(IN, $file)) {
print "*** CAN'T OPEN $ARGV[0] ***\n";
exit(1);
} else {
binmode(IN);
$file_size = -s $file;
}
# pid list
my(@pmt_pid_list) = ();
my(@es_pid_list) = ();
my($nit_pid_from_pmt) = undef;
# buffer for multi section table
my(@multi_section_tables);
my($buf);
my(@data);
my(@table_data);
my(%packet_buf_list) = ();
my(@table_list) = (); # for duplication check
# initial pid filter.
my @pid_list;
push(@pid_list, @gen_pid_list);
push(@pid_list, @pmt_pid_list);
push(@pid_list, @es_pid_list);
if (defined($nit_pid_from_pmt)) {
push(@pid_list, $nit_pid_from_pmt);
}
if (defined($polsat_epg_pid)) {
push(@pid_list, $polsat_epg_pid);
}
# read the file.
for (my $i = 0; $i < $count || 0 == $count; ++$i) {
my $n = sysread(IN, $buf, 188);
if ($n != 188) {
print "*** EOF($i packets) ***\n";
last;
}
if (0 < $i && 0 == $i % 100000) {
my $progress = (0 == $count) ?
($i * 18800 / $file_size) : ($i * 100 / $count);
printf "%d packets (%.2f%%) completed.\n",
$i, $progress;
}
# get data buffer
@data = unpack("C*", $buf);
my $tspacket = TSPacket->new;
$tspacket->SetBuffer(\@data);
################################
# TS Packet
################################
# ts header
my %ts_header = get_ts_header($tspacket);
dump_ts_header(%ts_header);
if ($ts_header{ synchronization_byte } != 0x47) {
print "*** ERROR PACKET ($i) ***\n";
#
# recovery mode
#
seek(IN, -188, 1); # rewind
my $skip = 0;
do {
$n = sysread(IN, $buf, 1);
if ($n != 1) {
print "*** FORCE CLOSE ***\n";
close(IN);
exit;
}
$skip += 1;
} while (0x47 != unpack("C1", $buf));
# back 1 byte
seek(IN, -1, 1); # back 1 byte from current position.
printf "*** SKIP %d bytes ***\n", $skip - 1;
next; # continue
}
my $pid = $ts_header{ pid };
my $idx = $ts_header{ continuity_index };
my $adp = $ts_header{ adaptation_field_control };
# filtering by PID
my $target_packet = 0;
foreach my $p (@pid_list) {
if ($p == $pid) {
$target_packet = 1;
last; # break
}
}
if (0 == $target_packet) {
printf "pid mismatch 0x%04X\n", $pid;
next; # continue
}
# for debug...
#printf "===== TS packet ";
#$tspacket->Dump();
#dump_ts_header(%ts_header);
# query PacketBuffer by PID
my $packet_buf = $packet_buf_list{ $pid };
unless (defined($packet_buf)) {
if (1 == $ts_header{ payload_unit_start_indicator }) {
# ***** VERY FIRST PACKET FOR THE PID. *****
# |<------- payload ------->|
# +------------------+---+-----------+---------+
# |(adaptation field)| N | xxxxxxxxx | new buf |
# +------------------+---+-----------+---------+
# ^ |<--- N --->|
# start_idx = pointer_field
$packet_buf = PacketBuffer->new;
my $start_idx = get_start_index_of_payload($adp, \@data);
my $pointer_field = $data[$start_idx]; # get 'N'
my $start_point = $start_idx + 1 + $pointer_field;
my @datax = @data[$start_point..$#data];
$packet_buf->Init($pid, $idx, \@datax);
$packet_buf_list{ $pid } = $packet_buf;
}
next; # continue
}
else {
if ($packet_buf->GetIndex() == $idx) {
# same index, just ignore.
next; # continue
}
elsif (2 == $adp) {
# only adaptation field, just ignore.
next; # continue
}
elsif ((($packet_buf->GetIndex() + 1) % 16) != $idx) {
# reset buffer
$packet_buf_list{ $pid } = undef;
next; # continue
}
else {
if (0 == $ts_header{ payload_unit_start_indicator }) {
my $start_idx = get_start_index_of_payload($adp, \@data);
my @datax = @data[$start_idx..$#data];
$packet_buf->Add($idx, \@datax);
next; # continue
} else {
# GET BUFFER OF PAYLOAD/ADDAPTATION FIELD
# |<-------- payload --------->|
# +------------------+---+-------------+----------+
# |(adaptation field)| N | current buf | next buf |
# +------------------+---+-------------+----------+
# ^ |<---- N ---->|
# start_idx = pointer_field
my $start_idx = get_start_index_of_payload($adp, \@data);
my $pointer_field = $data[$start_idx]; # get 'N'
my $next_start_point = $start_idx + 1 + $pointer_field;
my $current_end_point = $next_start_point - 1;
# >>> addition to current buffer
if (0 < $pointer_field) {
my @datac = @data[($start_idx + 1)..$current_end_point];
$packet_buf->Add($idx, \@datac);
}
@table_data = $packet_buf->GetBuffer();
# >>> initialization of next buffer
my @datax = @data[$next_start_point..$#data];
$packet_buf->Init($pid, $idx, \@datax);
# go SI/PSI
}
}
}
################################
# Payload of TS Packet
################################
my $table = TSPacket->new;
$table->SetBuffer(\@table_data);
################################
# SI/PSI
################################
until ($table->EOF() || 0xff == $table->PeekByte()) {
################################
# private section header
################################
my %private_sec = get_private_section($table);
#printf "===== Section ";
#$table->Dump();
#dump_private_section(%private_sec);
if (0 == $private_sec{ status }) {
print "*** SECTION LENGTH ERROR ($i) ***\n";
if (0 < $dump_err_packet) {
printf "===== Section ";
$table->Dump();
dump_private_section(%private_sec);
}
$table->SkipToNextSection();
next; # continue
}
################################
# Integrity check
################################
my $table_id = $private_sec{ table_id };
my $section_syntax = $private_sec{ section_syntax_indicator };
if (0 < $check_integrity) {
# PID vs section_syntax_indicator
if (0 == $section_syntax &&
(0x10 == $pid ||
0x11 == $pid ||
0x12 == $pid)) {
printf "*** INTEGRITY CHECK FAILED (%d) PID 0x%04X SYNTAX %d\n",
$i, $pid, $section_syntax;
if (0 < $dump_err_packet) {
printf "===== Section ";
$table->Dump();
dump_private_section(%private_sec);
}
$table->SkipToNextSection();
next; # continue
}
# PID vs table_id
}
################################
# CRC32
################################
if (0 < $check_crc32) {
if (1 == $private_sec{ section_syntax_indicator }) {
#$table->Dump();
my @body = $table->GetBody();
my @payload = @body[0..($#body - 4)];
my @crc32 = @body[($#body - 3)..$#body];
my $crc32 = ($crc32[0] << 24) +
($crc32[1] << 16) +
($crc32[2] << 8) +
($crc32[3] << 0);
my $crc32_of_payload = CRC32(\@payload);
if (1 < $check_crc32) {
printf "CRC32 field : 0x%08x\n", $crc32_of_payload;
printf "CRC32 calced : 0x%08x\n", $crc32;
}
if ($crc32 != $crc32_of_payload) {
print "*** CRC32 ERROR PACKET ($i) ***\n";
if (0 < $dump_err_packet) {
printf "===== Section ";
$table->Dump();
dump_private_section(%private_sec);
}
$table->SkipToNextSection();
next; # continue
}
}
else {
if (1 < $check_crc32) {
printf "NO CRC32 (section_syntax_indicator = 0)\n";
}
}
}
################################
# Duplication check
################################
my $table_id_extension = $private_sec{ table_extension_id };
my $section_number = $private_sec{ section_number };
my $version_number = $private_sec{ version_number };
if (0 < $check_version && 1 == $section_syntax) {
my $found_item = undef;
foreach my $item (@table_list) {
if ($item->Equal($table_id, $table_id_extension, $section_number)) {
$found_item = $item;
last; # break
}
}
if (defined($found_item)) {
if ($found_item->GetNextVersion() == $version_number) {
if (1 < $check_version) {
printf "*** new version %d (0x%02X,0x%04X,0x%02X,v%d) ***\n",
$i,
$table_id,
$table_id_extension,
$section_number,
$version_number;
}
$found_item->SetVersion($version_number);
} else {
if (1 < $check_version) {
printf "*** same or old table %d (0x%02X,0x%04X,0x%02X,v%d) ***\n",
$i,
$table_id,
$table_id_extension,
$section_number,
$version_number;
}
$table->SkipToNextSection();
next; # continue
}
} else {
if (1 < $check_version) {
printf "*** new table %d (0x%02X,0x%04X,0x%02X,v%d) ***\n",
$i,
$table_id,
$table_id_extension,
$section_number,
$version_number;
}
my $new_item = DuplicateChecker->new;
$new_item->Set($table_id,
$table_id_extension,
$section_number,
$version_number);
push(@table_list, $new_item);
}
}
################################
# Table parsing
################################
print "pid $pid\n";
# PAT
if ($pid == 0) {
dump_table_found("PAT", $pid, $i, \%private_sec);
my %pat = get_pat($table, %private_sec);
dump_pat(%pat);
@pmt_pid_list = get_pid_list_from_pat(%pat);
$nit_pid_from_pmt = get_nit_pid_from_pat(%pat);
# update pid list.
@pid_list = ();
push(@pid_list, @gen_pid_list);
push(@pid_list, @pmt_pid_list);
push(@pid_list, @es_pid_list);
if (defined($nit_pid_from_pmt)) {
push(@pid_list, $nit_pid_from_pmt);
}
if (defined($polsat_epg_pid)) {
push(@pid_list, $polsat_epg_pid);
}
$table->SkipToNextSection();
next; # continue
}
# PMT
foreach my $pmt_pid (@pmt_pid_list) {
if ($pmt_pid == $pid) {
dump_table_found("PMT", $pid, $i, \%private_sec);
#dump_ts_header(%ts_header);
#dump_private_section(%private_sec);
#$table->Dump();
my %pmt = get_pmt($table, %private_sec);
dump_pmt(%pmt);
last; # break
}
}
# NIT
if (0x10 == $pid ||
(defined($nit_pid_from_pmt) && $nit_pid_from_pmt == $pid)) {
my $name = (0x40 == $table_id ? "NITa" : "NITo");
dump_table_found($name, $pid, $i, \%private_sec);
#dump_ts_header(%ts_header);
#$table->Dump();
my %nit = get_nit($table, %private_sec);
dump_nit(%nit);
# update pid list.
@pid_list = ();
push(@pid_list, @gen_pid_list);
push(@pid_list, @pmt_pid_list);
push(@pid_list, @es_pid_list);
if (defined($nit_pid_from_pmt)) {
push(@pid_list, $nit_pid_from_pmt);
}
if (defined($polsat_epg_pid)) {
push(@pid_list, $polsat_epg_pid);
}
$table->SkipToNextSection();
next; # continue
}
# BAT
if (0x11 == $pid && 0x4A == $table_id) {
dump_table_found("BAT", $pid, $i, \%private_sec);
my %bat = get_bat($table, %private_sec);
dump_bat(%bat);
# update pid list.
@pid_list = ();
push(@pid_list, @gen_pid_list);
push(@pid_list, @pmt_pid_list);
push(@pid_list, @es_pid_list);
if (defined($nit_pid_from_pmt)) {
push(@pid_list, $nit_pid_from_pmt);
}
if (defined($polsat_epg_pid)) {
push(@pid_list, $polsat_epg_pid);
}
$table->SkipToNextSection();
next; # continue
}
# SDT
if (0x11 == $pid && (0x42 == $table_id || 0x46 == $table_id)) {
my $name = (0x42 == $table_id ? "SDTa" : "SDTo");
dump_table_found($name, $pid, $i, \%private_sec);
#dump_ts_header(%ts_header);
#dump_private_section(%private_sec);
#$table->Dump();
my %sdt = get_sdt($table, %private_sec);
dump_sdt(%sdt);
$table->SkipToNextSection();
next; # continue
}
# EIT
if (0x12 == $pid) {
my $name = "EIT";
if (0x4e == $table_id) { $name = "EITpf(a)"; }
elsif (0x4f == $table_id) { $name = "EITpf(o)"; }
elsif (0x50 <= $table_id &&
$table_id <= 0x5f) { $name = "EITsch(a)"; }
elsif (0x60 <= $table_id &&
$table_id <= 0x6f) { $name = "EITsch(o)"; }
dump_table_found($name, $pid, $i, \%private_sec);
#dump_private_section(%private_sec);
#$table->Dump();
my %eit = get_eit($table, %private_sec);
dump_eit(%eit);
$table->SkipToNextSection();
next; # continue
}
# TOT/TDT
if (0x14 == $pid) {
dump_table_found("TOT/TDT", $pid, $i, \%private_sec);
my %tot_tdt = get_tot_tdt($table, %private_sec);
dump_tot_tdt(%tot_tdt);
$table->SkipToNextSection();
next; # continue
}
# CLT (for Polsat)
if (0xfa == $table_id) {
dump_table_found("CLT", $pid, $i, \%private_sec);
my %clt = get_clt($table, %private_sec);
dump_clt(%clt);
$table->SkipToNextSection();
next; # continue
}
# PDT (for Polsat)
if (0xfb == $table_id) {
dump_table_found("PDT", $pid, $i, \%private_sec);
my %pdt = get_pdt($table, %private_sec);
dump_pdt(%pdt);
$table->SkipToNextSection();
next; # continue
}
$table->SkipToNextSection();
}
}
# close the file.
close(IN);
}
##########################################################################
#
# PARSER
#
##########################################################################
#=========================================================================
# TS header
#=========================================================================
sub get_ts_header {
my $p = shift;
return (
synchronization_byte => $p->GetByte(),
transport_error_indicator => $p->Get( 1),
payload_unit_start_indicator => $p->Get( 1),
transport_priority => $p->Get( 1),
pid => $p->Get(13),
transport_scrambling_control => $p->Get( 2),
adaptation_field_control => $p->Get( 2),
continuity_index => $p->Get( 4)
);
}
#=========================================================================
# Private Section
#=========================================================================
sub get_private_section {
my $p = shift;
my $ok = $p->SetHeadPointer();
if (!$ok) {
return { status => 0 }; # no name hash.
}
my %section_head = (
table_id => $p->GetByte(),
section_syntax_indicator => $p->Get( 1),
reserved_0 => $p->Get( 1),
reserved_1 => $p->Get( 2),
section_length => $p->Get(12),
);
if (1 == $section_head{ section_syntax_indicator }) {
my %section_ext = (
table_extension_id => $p->GetWord(),
reserved_2 => $p->Get( 2),
version_number => $p->Get( 5),
current_next_indicator => $p->Get( 1),
section_number => $p->GetByte(),
last_section_number => $p->GetByte()
);
# 5 ... byte length from table_extension_id to last_section_number
# 4 ... byte length of CRC32
$section_head{ payload_length } = $section_head{ section_length } - 5 - 4;
$p->SetSectionLength ($section_head{ payload_length });
$ok = $p->SetBodyLength($section_head{ section_length } - 5);
$section_head{ status } = $ok ? 1 : 0;
my %section = (%section_head, %section_ext); # join hash tables
return %section;
}
else {
my %section_ext = (
table_extension_id => 0,
version_number => 0,
current_next_indicator => 0,
section_number => 0,
last_section_number => 0
);
# << CAUTION >>
# payload_length may contain CRC32.
# - TDT does not have CRC32.
# - TOT have CRC32. This case, payload_length contains CRC32.
$section_head{ payload_length } = $section_head{ section_length };
$p->SetSectionLength ($section_head{ payload_length });
$ok = $p->SetBodyLength($section_head{ payload_length });
$section_head{ status } = $ok ? 1 : 0;
my %section = (%section_head, %section_ext); # join hash tables
return %section;
}
}
#=========================================================================
# descriptor
#=========================================================================
sub get_descriptor {
my $p = shift;
my $length = shift; # total length of descriptor
$p->SetDescriptorLength($length);
my @descriptor_list = ();
while (0 < $p->GetRemainBytesOfDescriptor()) {
my %descriptor = (
descriptor_tag => $p->GetByte(),
descriptor_length => $p->GetByte(),
);
my @desc_buf = $p->GetBytes($descriptor{ descriptor_length });
$descriptor{ desc_buf } = \@desc_buf;
push(@descriptor_list, \%descriptor);
# printf " - desc : tag 0x%02x len %d\n",
# $descriptor{ descriptor_tag },
# $descriptor{ descriptor_length };
}
return \@descriptor_list; # returns reference to the list.
}
sub get_descriptor_by_num {
my $p = shift;
my $count = shift; # number of descriptor
my @descriptor_list = ();
while (0 < $count) {
my %descriptor = (
descriptor_tag => $p->GetByte(),
descriptor_length => $p->GetByte(),
);
my @desc_buf = $p->GetBytes($descriptor{ descriptor_length });
$descriptor{ desc_buf } = \@desc_buf;
push(@descriptor_list, \%descriptor);
# printf " - desc : tag 0x%02x len %d\n",
# $descriptor{ descriptor_tag },
# $descriptor{ descriptor_length };
--$count;
}
return \@descriptor_list; # returns reference to the list.
}
#=========================================================================
# PAT
#=========================================================================
sub get_pat {
my $p = shift;
my %pat = @_;
# Program loop
my @program_list = ();
while (0 < $p->GetRemainBytesOfSection()) {
my %program = (
program_number => $p->GetWord(),
reserved => $p->Get(3),
PID => $p->Get(13),
);
push(@program_list, \%program); # add reference to hash.
}
$pat{ program_list } = \@program_list; # set reference to array.
return %pat;
}
#=========================================================================
# PMT
#=========================================================================
sub get_pmt {
my $p = shift;
my %pmt = @_;
$pmt{ reserved_3 } = $p->Get( 3);
$pmt{ PCR_PID } = $p->Get(13);
$pmt{ reserved_4 } = $p->Get( 4);
$pmt{ program_info_length } = $p->Get(12);
$pmt{ descriptor_program } = get_descriptor($p, $pmt{ program_info_length });
# ES loop
my @es_list = ();
while (0 < $p->GetRemainBytesOfSection()) {
my %es = (
stream_type => $p->GetByte(),
reserved_1 => $p->Get( 3),
elementary_PID => $p->Get(13),
reserved_2 => $p->Get( 4),
descriptor_length => $p->Get(12)
);
$es{ descriptor } = get_descriptor($p, $es{ descriptor_length });
push(@es_list, \%es); # add reference to hash.
if (0 < $dump_summary) {
printf "#%d pgm 0x%04X(%d) type 0x%02X ES_PID 0x%04X\n",
$#es_list + 1,
$pmt{ table_extension_id },
$pmt{ table_extension_id },
$es{ stream_type },
$es{ elementary_PID };
}
}
$pmt{ es_list } = \@es_list; # set refernece to array.
return %pmt;
}
#=========================================================================
# NIT
#=========================================================================
sub get_nit {
my $p = shift;
my %nit = @_;
$nit{ reserved_3 } = $p->Get( 4);
$nit{ descriptor_length } = $p->Get(12);
$nit{ descriptor } = get_descriptor($p, $nit{ descriptor_length });
$nit{ reserved_4 } = $p->Get(4);
$nit{ tarnsport_stream_loop_length } = $p->Get(12);
# Network loop
my @network_list = ();
while (0 < $p->GetRemainBytesOfSection()) {
my %network = (
transport_stream_id => $p->GetWord(),
original_network_id => $p->GetWord(),
reserved => $p->Get( 4),
descriptor_length => $p->Get(12)
);
$network{ descriptor } =
get_descriptor($p, $network{ descriptor_length });
push(@network_list, \%network); # add reference to Hash.
if (0 < $dump_summary) {
printf "#%d tsid 0x%04x onid 0x%04x\n",
$#network_list + 1,
$network{ transport_stream_id },
$network{ original_network_id };
}
}
$nit{ network_list } = \@network_list;
return %nit;
}
#=========================================================================
# BAT
#=========================================================================
sub get_bat {
my $p = shift;
my %bat = @_;
$bat{ reserved_3 } = $p->Get( 4);
$bat{ descriptor_length } = $p->Get(12);
$bat{ descriptor } = get_descriptor($p, $bat{ descriptor_length });
$bat{ reserved_4 } = $p->Get(4);
$bat{ tarnsport_stream_loop_length } = $p->Get(12);
# TS loop
my @ts_list = ();
while (0 < $p->GetRemainBytesOfSection()) {
my %ts = (
transport_stream_id => $p->GetWord(),
original_network_id => $p->GetWord(),
reserved => $p->Get( 4),
descriptor_length => $p->Get(12)
);
$ts{ descriptor } =
get_descriptor($p, $ts{ descriptor_length });
push(@ts_list, \%ts); # add reference to Hash.
if (0 < $dump_summary) {
printf "#%d tsid 0x%04x onid 0x%04x\n",
$#ts_list + 1,
$ts{ transport_stream_id },
$ts{ original_network_id };
}
}
$bat{ ts_list } = \@ts_list;
return %bat;
}
#=========================================================================
# SDT
#=========================================================================
sub get_sdt {
my $p = shift;
my %sdt = @_;
$sdt{ original_network_id } = $p->GetWord();
$sdt{ reserved_3 } = $p->GetByte();
# Service loop
my @service_list = ();
while (0 < $p->GetRemainBytesOfSection()) {
my %service = (
service_id => $p->GetWord(),
reserved => $p->Get( 6),
EIT_schedule_flag => $p->Get( 1),
EIT_present_following_flag => $p->Get( 1),
running_status => $p->Get( 3),
free_CA_mode => $p->Get( 1),
descriptor_length => $p->Get(12)
);
$service{ descriptor } =
get_descriptor($p, $service{ descriptor_length });
push(@service_list, \%service);
if (0 < $dump_summary) {
printf "#%d tsid 0x%04X onid 0x%04X svcid 0x%04X \n",
$#service_list + 1,
$sdt{ table_extension_id },
$sdt{ original_network_id },
$service{ service_id };
}
}
$sdt{ service_list } = \@service_list;
return %sdt;
}
#=========================================================================
# EIT
#=========================================================================
sub get_eit {
my $p = shift;
my %eit = @_;
$eit{ transport_stream_id } = $p->GetWord();
$eit{ original_network_id } = $p->GetWord();
$eit{ segment_last_section_number } = $p->GetByte();
$eit{ last_table_id } = $p->GetByte();
# Event loop
my @event_list = ();
while (0 < $p->GetRemainBytesOfSection()) {
my %event = (
event_id => $p->GetWord(),
start_time_mjd => $p->Get(16), # Modified Jullian Date
start_time_utc => $p->Get(24), # 6 digits 4-bit BCD
duration => $p->Get(24), # 6 digits 4-bit BCD
running_status => $p->Get( 3),
free_CA_mode => $p->Get( 1),
descriptor_length => $p->Get(12)
);
$event{ descriptor } =
get_descriptor($p, $event{ descriptor_length });
push(@event_list, \%event);
if (0 < $dump_summary) {
printf "#%d tsid 0x%04X onid 0x%04X svcid 0x%04X eventid 0x%04X\n",
$#event_list + 1,
$eit{ transport_stream_id },
$eit{ original_network_id },
$eit{ table_extension_id },
$event{ event_id };
}
}
$eit { event_list } = \@event_list;
return %eit;
}
#=========================================================================
# TOT/TDT
#=========================================================================
sub get_tot_tdt {
my $p = shift;
my %tot_tdt = @_;
$tot_tdt{ UTC_time_mjd } = $p->Get(16); # Modified Jullian Date
$tot_tdt{ UTC_time_utc } = $p->Get(24); # 6 digits 4-bit BCD
# TOT
if (0x73 == $tot_tdt{ table_id }) {
$p->Skip(4);
$tot_tdt{ descriptor_length } = $p->Get(12);
$tot_tdt{ descriptor } = get_descriptor($p, $tot_tdt{ descriptor_length });
}
return %tot_tdt;
}
#=========================================================================
# CLT
#=========================================================================
sub get_clt {
my $p = shift;
my %clt = @_;
$clt{ current_day } = $p->GetWord();
$clt{ how_many_days_epg } = $p->Get(4);
$clt{ languages } = $p->Get(4);
# Program loop
my @program_list = ();
my $index = 0;
while (0 < $p->GetRemainBytesOfSection()) {
my %program = (
program_index => $index,
original_network_id => $p->GetWord(),
transport_stream_id => $p->GetWord(),
service_id => $p->GetWord(),
descriptor_count => $p->GetWord(),
);
$program{ descriptor } =
get_descriptor_by_num($p, $program{ descriptor_count });
push(@program_list, \%program);
++$index;
if (0 < $dump_summary) {
printf "#%d day %d days %d lang %d tsid 0x%04X onid 0x%04X svcid 0x%04X\n",
$#program_list + 1,
$clt{ current_day },
$clt{ how_many_days_epg },
$clt{ languages },
$program{ transport_stream_id },
$program{ original_network_id },
$program{ service_id },
}
}
$clt{ program_list } = \@program_list;
return %clt;
}
#=========================================================================
# PDT
#=========================================================================
sub get_pdt {
my $p = shift;
my %pdt = @_;
if (0 == $pdt{ section_number }) {
$pdt{ compression_flag } = $p->Get(1);
$p->Skip(7);
} else {
$pdt{ compression_flag } = undef;
}
my @pis = $p->GetBytes($p->GetRemainBytesOfSection());
$pdt{ ref_pis } = \@pis;
return %pdt;
}
##########################################################################
#
# DUMP (Section)
#
##########################################################################
#=========================================================================
# dump TS header
#=========================================================================
sub dump_ts_header {
my %ts_header = @_;
printf "===== TS HEADER BEGIN =====\n";
printf "- synchronization_byte 0x%02X\n", $ts_header{ synchronization_byte };
printf "- transport_error_indicator %d \n", $ts_header{ transport_error_indicator };
printf "- payload_unit_start_indicator %d \n", $ts_header{ payload_unit_start_indicator };
printf "- transport_priority %d \n", $ts_header{ transport_priority };
printf "- pid 0x%04X\n", $ts_header{ pid };
printf "- transport_scrambling_control %d \n", $ts_header{ transport_scrambling_control };
printf "- adaptation_field_control %d \n", $ts_header{ adaptation_field_control };
printf "- continuity_index %d \n", $ts_header{ continuity_index };
printf "===== TS HEADER END =======\n";