-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.c
1511 lines (1400 loc) · 43.5 KB
/
dump.c
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
/* dump -- tool to dump stats regarding a dvb stream from a file
*
* Copyright (C) 2009-2010 James Courtier-Dutton <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#if 1
#define TS_LOG 1
#define TS_SCRAM 1
#define TS_PAT_LOG 1
#define TS_PMT_LOG 1
#define TS_HEADER_LOG 1
#endif
#define TS_PAT_LOG 1
#define TS_PMT_LOG 1
#define TS_SCRAM 1
/* more PIDS are needed due "auto-detection". 40 spare media entries */
#define PKT_SIZE 188
#define BODY_SIZE (188 - 4)
#define MAX_PIDS ((BODY_SIZE - 1 - 13) / 4) + 40
#define MAX_PMTS ((BODY_SIZE - 1 - 13) / 4) + 10
#define SYNC_BYTE 0x47
/* more PIDS are needed due "auto-detection". 40 spare media entries */
#define NULL_PID 0x1fff
#define INVALID_PID ((unsigned int)(-1))
#define INVALID_PROGRAM ((unsigned int)(-1))
#define INVALID_CC ((unsigned int)(-1))
#define PID_TYPE_NONE 0
#define PID_TYPE_PAT 1
#define PID_TYPE_PMT 2
#define PID_TYPE_CA_ECM 3
#define PID_TYPE_CA_EMM 4
#define PID_TYPE_NIT 5
#define PID_TYPE_AUDIO_MPEG 6
#define PID_TYPE_AUDIO_AC3 7
#define PID_TYPE_VIDEO_MPEG2 8
#define PID_TYPE_VIDEO_H264 9
#define PID_TYPE_PRIV_SECT 10
#define PID_TYPE_PRIV_PES 11
#define PID_TYPE_UNKNOWN 12
char *type[] = {"NONE",
"PAT",
"PMT",
"CA_ECM",
"CA_EMM",
"NIT",
"AUDIO_MPEG",
"AUDIO_AC3",
"VIDEO_MPEG2",
"VIDEO_H264",
"PRIV_SECT",
"PRIV_PES",
"UNKNOWN"};
uint8_t buffer[200];
struct section_s {
int size;
int progress;
uint8_t *buffer;
};
struct pid_s {
int present;
int scrambling_control;
int program_count;
int type;
uint8_t last_continuity_counter;
uint8_t *previous_ecm;
struct section_s section;
int pid_for_ecm;
};
struct video_pid_s {
int pid;
int ca_pid;
};
struct audio_pid_s {
int pid;
int ca_pid;
};
struct service_s {
int program_id;
char *provider;
char *name;
};
struct program_s {
int program_id;
int pid_pmt;
struct video_pid_s video;
struct audio_pid_s audio;
int service_count;
};
/*
* Describe a single elementary stream.
*/
struct demux_ts_s {
// unsigned int pid;
// fifo_buffer_t *fifo;
// uint8_t *content;
// uint32_t size;
// uint32_t type;
// int64_t pts;
// buf_element_t *buf;
// unsigned int counter;
// uint16_t descriptor_tag; /* +0x100 for PES stream IDs (no available TS descriptor tag?) */
// int64_t packet_count;
// int corrupted_pes;
// uint32_t buffered_bytes;
// int autodetected;
uint32_t crc32_table[256];
uint32_t program_number[MAX_PMTS];
uint32_t pmt_pid[MAX_PMTS];
int number_of_programs;
struct program_s *programs;
struct service_s *services;
struct pid_s *pids;
int *pmt[MAX_PMTS];
uint8_t *pmt_write_ptr[MAX_PMTS];
int audio_tracks_count;
unsigned int videoPid;
uint32_t last_pmt_crc;
unsigned int spu_pid;
};
struct demux_ts_s demux_ts;
uint8_t pat[200];
static void demux_ts_build_crc32_table(struct demux_ts_s *this) {
uint32_t i, j, k;
for( i = 0 ; i < 256 ; i++ ) {
k = 0;
for (j = (i << 24) | 0x800000 ; j != 0x80000000 ; j <<= 1) {
k = (k << 1) ^ (((k ^ j) & 0x80000000) ? 0x04c11db7 : 0);
}
this->crc32_table[i] = k;
}
}
static uint32_t demux_ts_compute_crc32(struct demux_ts_s *this, uint8_t *data,
int32_t length, uint32_t crc32) {
int32_t i;
for(i = 0; i < length; i++) {
crc32 = (crc32 << 8) ^ this->crc32_table[(crc32 >> 24) ^ data[i]];
}
return crc32;
}
static int ecm_compare(uint8_t *previous, uint8_t *buffer)
{
uint8_t *original_pkt = buffer;
uint8_t *pkt;
uint8_t adaptation_field_control;
int ret;
int data_offset;
int ecm_offset;
int tmp;
uint32_t table_id;
uint32_t section_syntax_indicator;
int32_t section_length;
int n;
int match;
ret = 0;
data_offset = 4;
adaptation_field_control = (original_pkt[3] >> 4) & 0x03;
if( adaptation_field_control & 0x2 ){
uint32_t adaptation_field_length = original_pkt[4];
/*
* Skip adaptation header.
*/
data_offset += adaptation_field_length + 1;
}
pkt = original_pkt + data_offset - 4;
ecm_offset = data_offset - 4;
/*
* sections start with a pointer. Skip it!
*/
tmp = pkt[4];
ecm_offset += tmp;
pkt += tmp;;
table_id = (unsigned int)pkt[5] ;
section_syntax_indicator = (((unsigned int)pkt[6] >> 7) & 1) ;
section_length = (((unsigned int)pkt[6] & 0x03) << 8) | pkt[7];
for(n = 0; n < section_length + 3; n++) {
printf("%02x ", previous[ecm_offset + n + 5 ]);
if ((n % 32) == 31) {
printf("\n");
}
}
printf("\n");
for(n = 0; n < section_length + 3; n++) {
printf("%02x ", buffer[ecm_offset + n + 5 ]);
if ((n % 32) == 31) {
printf("\n");
}
}
printf("\n");
match = 0;
for(n = 0; n < section_length + 3; n++) {
if (buffer[ecm_offset + n + 5] != previous[ecm_offset + n + 5 ]) {
ret = 1;
break;
}
}
return ret;
}
static void demux_ts_parse_ecm (struct demux_ts_s *this, uint8_t *original_pkt,
uint32_t offset, unsigned int pusi) {
uint32_t table_id;
uint32_t section_syntax_indicator;
int32_t section_length;
uint32_t transport_stream_id;
uint32_t version_number;
uint32_t current_next_indicator;
uint32_t section_number;
uint32_t last_section_number;
uint32_t desc_tag;
uint32_t desc_len;
uint32_t ca_system_id;
uint32_t ca_pid;
uint32_t crc32;
uint32_t calc_crc32;
uint32_t pid;
uint8_t *program_offset;
unsigned char *program;
unsigned int program_number;
unsigned int pmt_pid;
unsigned int program_count;
int count;
int n;
uint8_t *pkt;
/*
* A PAT in a single section should start with a payload unit start
* indicator set.
*/
printf ("demux_ts: parsing ECM\n");
if (!pusi) {
printf ("demux_ts: demux error! ECM without payload unit start indicator\n");
return;
}
pkt = original_pkt + offset;
/*
* sections start with a pointer. Skip it!
*/
pkt += pkt[4];
if (pkt - original_pkt > PKT_SIZE) {
printf ("demux_ts: demux error! CAT with invalid pointer\n");
return;
}
table_id = (unsigned int)pkt[5] ;
section_syntax_indicator = (((unsigned int)pkt[6] >> 7) & 1) ;
section_length = (((unsigned int)pkt[6] & 0x03) << 8) | pkt[7];
#ifdef TS_PAT_LOG
printf ("demux_ts: ECM table_id: %.2x\n", table_id);
printf (" section_syntax: %d\n", section_syntax_indicator);
printf (" section_length: %d (%#.3x)\n",
section_length, section_length);
#endif
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32 (this, pkt+5, section_length+3-4,
0xffffffff);
#ifdef TS_PAT_LOG
printf ("demux_ts: ECM CRC32: %.8x\n", calc_crc32);
#endif
pid = ((original_pkt[1] << 8) |
original_pkt[2]) & 0x1fff;
printf("demux_ts:ts_header:pid:0x%.4x\n", pid);
for(n = 0; n < section_length + 3 + 8; n++) {
printf("%02x ", pkt[n + 5 ]);
if ((n % 32) == 31) {
printf("\n");
}
}
printf("\n");
}
/*
* demux_ts_parse_cat
*
* Parse a conditional access table (CAT).
* The CAT is expected to be exactly one section long,
* and that section is expected to be contained in a single TS packet.
*
* The CAT is assumed to contain a single program definition, though
* we can cope with the stupidity of SPTSs which contain NITs.
*/
static void demux_ts_parse_cat (struct demux_ts_s *this, uint8_t *original_pkt,
uint32_t offset, unsigned int pusi) {
uint32_t table_id;
uint32_t section_syntax_indicator;
int32_t section_length;
uint32_t transport_stream_id;
uint32_t version_number;
uint32_t current_next_indicator;
uint32_t section_number;
uint32_t last_section_number;
uint32_t desc_tag;
uint32_t desc_len;
uint32_t ca_system_id;
uint32_t ca_pid;
uint32_t crc32;
uint32_t calc_crc32;
uint8_t *program_offset;
unsigned char *program;
unsigned int program_number;
unsigned int pmt_pid;
unsigned int program_count;
int count;
int n;
uint8_t *pkt;
/*
* A PAT in a single section should start with a payload unit start
* indicator set.
*/
printf ("demux_ts: parsing CAT\n");
if (!pusi) {
printf ("demux_ts: demux error! CAT without payload unit start indicator\n");
return;
}
pkt = original_pkt + offset;
/*
* sections start with a pointer. Skip it!
*/
pkt += pkt[4];
if (pkt - original_pkt > PKT_SIZE) {
printf ("demux_ts: demux error! CAT with invalid pointer\n");
return;
}
table_id = (unsigned int)pkt[5] ;
section_syntax_indicator = (((unsigned int)pkt[6] >> 7) & 1) ;
section_length = (((unsigned int)pkt[6] & 0x03) << 8) | pkt[7];
transport_stream_id = ((uint32_t)pkt[8] << 8) | pkt[9];
version_number = ((uint32_t)pkt[10] >> 1) & 0x1f;
current_next_indicator = ((uint32_t)pkt[10] & 0x01);
section_number = (uint32_t)pkt[11];
last_section_number = (uint32_t)pkt[12];
crc32 = (uint32_t)pkt[4+section_length] << 24;
crc32 |= (uint32_t)pkt[5+section_length] << 16;
crc32 |= (uint32_t)pkt[6+section_length] << 8;
crc32 |= (uint32_t)pkt[7+section_length] ;
#ifdef TS_PAT_LOG
printf ("demux_ts: CAT table_id: %.2x\n", table_id);
printf (" section_syntax: %d\n", section_syntax_indicator);
printf (" section_length: %d (%#.3x)\n",
section_length, section_length);
printf (" transport_stream_id: %#.4x\n", transport_stream_id);
printf (" version_number: %d\n", version_number);
printf (" c/n indicator: %d\n", current_next_indicator);
printf (" section_number: %d\n", section_number);
printf (" last_section_number: %d\n", last_section_number);
#endif
if ((section_syntax_indicator != 1) || !(current_next_indicator)) {
return;
}
if (pkt - original_pkt > BODY_SIZE - 1 - 3 - section_length) {
printf ("demux_ts: FIXME: (unsupported )PAT spans multiple TS packets\n");
return;
}
if ((section_number != 0) || (last_section_number != 0)) {
printf ("demux_ts: FIXME: (unsupported) PAT consists of multiple (%d) sections\n", last_section_number);
return;
}
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32 (this, pkt+5, section_length+3-4,
0xffffffff);
if (crc32 != calc_crc32) {
printf ("demux_ts: demux error! PAT with invalid CRC32: packet_crc32: %.8x calc_crc32: %.8x\n",
crc32,calc_crc32);
return;
}
#ifdef TS_PAT_LOG
else {
printf ("demux_ts: CAT CRC32: %.8x ok.\n", crc32);
}
#endif
for (n = 0; n < section_length - 9 ; ) {
desc_tag = pkt[13 + n];
desc_len = pkt[13 + n + 1];
ca_system_id = 0;
ca_pid = 0;
/* Handle CA */
if (desc_tag == 9) {
ca_system_id = (((uint32_t) pkt[13 + n + 2] << 8) |
pkt[13 + n + 3]);
ca_pid = (((uint32_t) pkt[13 + n + 4] << 8) |
pkt[13 + n + 5]) & 0x1fff;
this->pids[ca_pid].type = PID_TYPE_CA_EMM;
}
printf (" desc_tag: 0x%02x\n", desc_tag);
printf (" desc_len: 0x%02x\n", desc_len);
if (desc_tag == 9) {
printf (" ca_system_id: 0x%04x\n", ca_system_id);
printf (" ca_pid: 0x%04x\n", ca_pid);
}
printf("n = %d\n", n);
n += desc_len + 2;
printf("n + desc_len + 2 = %d\n", n);
}
printf("\n");
}
/*
* demux_ts_parse_pat
*
* Parse a program association table (PAT).
* The PAT is expected to be exactly one section long,
* and that section is expected to be contained in a single TS packet.
*
* The PAT is assumed to contain a single program definition, though
* we can cope with the stupidity of SPTSs which contain NITs.
*/
static void demux_ts_parse_pat (struct demux_ts_s *this, uint8_t *original_pkt,
uint32_t offset, unsigned int pusi) {
uint32_t table_id;
uint32_t section_syntax_indicator;
int32_t section_length;
uint32_t transport_stream_id;
uint32_t version_number;
uint32_t current_next_indicator;
uint32_t section_number;
uint32_t last_section_number;
uint32_t crc32;
uint32_t calc_crc32;
uint8_t *program_offset;
unsigned char *program;
unsigned int program_number;
unsigned int pmt_pid;
unsigned int program_count;
int count;
int n;
uint8_t *pkt;
/*
* A PAT in a single section should start with a payload unit start
* indicator set.
*/
printf ("demux_ts: parsing PAT\n");
if (!pusi) {
printf ("demux_ts: demux error! PAT without payload unit start indicator\n");
return;
}
pkt = original_pkt + offset;
/*
* sections start with a pointer. Skip it!
*/
pkt += pkt[4];
if (pkt - original_pkt > PKT_SIZE) {
printf ("demux_ts: demux error! PAT with invalid pointer\n");
return;
}
table_id = (unsigned int)pkt[5] ;
section_syntax_indicator = (((unsigned int)pkt[6] >> 7) & 1) ;
section_length = (((unsigned int)pkt[6] & 0x03) << 8) | pkt[7];
transport_stream_id = ((uint32_t)pkt[8] << 8) | pkt[9];
version_number = ((uint32_t)pkt[10] >> 1) & 0x1f;
current_next_indicator = ((uint32_t)pkt[10] & 0x01);
section_number = (uint32_t)pkt[11];
last_section_number = (uint32_t)pkt[12];
crc32 = (uint32_t)pkt[4+section_length] << 24;
crc32 |= (uint32_t)pkt[5+section_length] << 16;
crc32 |= (uint32_t)pkt[6+section_length] << 8;
crc32 |= (uint32_t)pkt[7+section_length] ;
#ifdef TS_PAT_LOG
printf ("demux_ts: PAT table_id: %.2x\n", table_id);
printf (" section_syntax: %d\n", section_syntax_indicator);
printf (" section_length: %d (%#.3x)\n",
section_length, section_length);
printf (" transport_stream_id: %#.4x\n", transport_stream_id);
printf (" version_number: %d\n", version_number);
printf (" c/n indicator: %d\n", current_next_indicator);
printf (" section_number: %d\n", section_number);
printf (" last_section_number: %d\n", last_section_number);
#endif
if ((section_syntax_indicator != 1) || !(current_next_indicator)) {
return;
}
if (pkt - original_pkt > BODY_SIZE - 1 - 3 - section_length) {
printf ("demux_ts: FIXME: (unsupported )PAT spans multiple TS packets\n");
return;
}
if ((section_number != 0) || (last_section_number != 0)) {
printf ("demux_ts: FIXME: (unsupported) PAT consists of multiple (%d) sections\n", last_section_number);
return;
}
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32 (this, pkt+5, section_length+3-4,
0xffffffff);
if (crc32 != calc_crc32) {
printf ("demux_ts: demux error! PAT with invalid CRC32: packet_crc32: %.8x calc_crc32: %.8x\n",
crc32,calc_crc32);
return;
}
#ifdef TS_PAT_LOG
else {
printf ("demux_ts: PAT CRC32 ok.\n");
}
#endif
/*
* Process all programs in the program loop.
*/
printf("section_length - 9 = %d\n", section_length - 9);
program_offset = pkt + 13;
count = (section_length - 9) / 4;
program_count = 0;
for (n = 0; n < (section_length - 9); n += 4) {
program_number = ((unsigned int)program_offset[n] << 8) | program_offset[n + 1];
pmt_pid = (((unsigned int)program_offset[n + 2] & 0x1f) << 8) | program_offset[n + 3];
/*
* completely skip NIT pids.
*/
// if (program_number == 0x0000)
// continue;
/*
* If we have yet to learn our program number, then learn it,
* use this loop to eventually add support for dynamically changing
* PATs.
*/
program_count = 0;
while ((this->programs[program_count].program_id != INVALID_PROGRAM) &&
(this->programs[program_count].program_id != program_number) &&
(program_count < 255 ) ) {
program_count++;
}
this->programs[program_count].program_id = program_number;
this->programs[program_count].pid_pmt = pmt_pid;
this->pids[pmt_pid].program_count = program_count;
if (program_number) {
this->pids[pmt_pid].type = PID_TYPE_PMT;
} else {
this->pids[pmt_pid].type = PID_TYPE_NIT;
}
#ifdef TS_PAT_LOG
if (this->programs[program_count].program_id != INVALID_PROGRAM)
printf ("demux_ts: PAT acquired count=%d programNumber=0x%04x "
"pmtPid=0x%04x\n",
program_count,
this->programs[program_count].program_id,
this->programs[program_count].pid_pmt);
#endif
}
}
/*
* NAME demux_ts_parse_pmt
*
* Parse a PMT. The PMT is expected to be exactly one section long,
* and that section is expected to be contained in a single TS packet.
*
* In other words, the PMT is assumed to describe a reasonable number of
* video, audio and other streams (with descriptors).
* FIXME: Implement support for multi section PMT.
*/
static void demux_ts_parse_section (struct demux_ts_s *this,
uint8_t *original_pkt,
uint32_t offset,
unsigned int pusi,
struct section_s *section,
int discontinuity)
{
uint32_t table_id;
uint32_t section_syntax_indicator;
uint32_t section_length = 0; /* to calm down gcc */
uint32_t program_number;
uint32_t version_number;
uint32_t current_next_indicator;
uint32_t section_number;
uint32_t last_section_number;
uint32_t reserved1;
uint32_t pcr_pid;
uint32_t reserved2;
uint32_t program_info_length;
uint32_t crc32;
uint32_t calc_crc32;
uint32_t coded_length;
unsigned int pid;
unsigned char *stream;
unsigned int i;
int count;
char *ptr = NULL;
unsigned char len;
uint32_t tmp32;
uint32_t offset_section_start;
uint8_t *pkt;
uint32_t progress;
/*
* A new section should start with the payload unit start
* indicator set. We allocate some mem (max. allowed for a PM section)
* to copy the complete section into one chunk.
*/
if (pusi) {
/* pointer to start of section. */
/* Only exists if pusi is set. */
tmp32 = original_pkt[offset + 4];
offset_section_start = offset + tmp32 + 5;
pkt = original_pkt + offset_section_start;
table_id = pkt[0] ;
section_syntax_indicator = (pkt[1] >> 7) & 0x01;
section_length = (((uint32_t) pkt[1] << 8) | pkt[2]) & 0x03ff;
program_number = ((uint32_t) pkt[3] << 8) | pkt[4];
version_number = (pkt[5] >> 1) & 0x1f;
current_next_indicator = pkt[5] & 0x01;
section_number = pkt[6];
last_section_number = pkt[7];
pcr_pid = (((uint32_t) pkt[8] << 8) | pkt[9]) & 0x1fff;
program_info_length = (((uint32_t) pkt[10] << 8) | pkt[11]) & 0x0fff;
/* Exclude program number 0, it is a NIT and not a PMT. */
section->size = section_length;
section->progress = 188-offset_section_start;
if (!section->buffer) {
section->buffer = calloc(1024, 1);
if (!section->buffer) {
printf("OUT OF MEMORY!!!!\n");
return;
}
}
memcpy (section->buffer, pkt, 188 - offset_section_start);
#ifdef TS_PMT_LOG
printf ("demux_ts: SECTION table_id: %2x\n", table_id);
printf (" section_syntax: %d\n", section_syntax_indicator);
printf (" section_length: %d (%#.3x)\n",
section_length, section_length);
printf (" program_number: %#.4x\n", program_number);
printf (" version_number: %d\n", version_number);
printf (" c/n indicator: %d\n", current_next_indicator);
printf (" section_number: %d\n", section_number);
printf (" last_section_number: %d\n", last_section_number);
printf (" pcr_pid: 0x%04x\n", pcr_pid);
printf (" program_info_length: 0x%04x\n", program_info_length);
printf (" progress: %d\n", section->progress);
#endif
if ((section_syntax_indicator != 1) || !current_next_indicator) {
#ifdef TS_PMT_LOG
printf ("ts_demux: section_syntax_indicator != 1 "
"|| !current_next_indicator\n");
#endif
return;
}
} else {
printf ("demux_ts: section !pusi\n");
if (discontinuity) {
section->size = 0;
printf ("demux_ts: section !pusi discontinuity\n");
return;
}
/* Wait for pusi */
if (!(section->size)) {
return;
}
section_length = section->size;
progress = section->progress;
len = 188 - offset - 4;
printf("!pusi: offset = 0x%04x len = 0x%04x\n", offset, len);
memcpy (section->buffer + progress, original_pkt + offset + 4, len);
section->progress += len;
}
}
static void process_sdt_descriptors(struct demux_ts_s *this, struct service_s *service, uint8_t *buffer, int len)
{
int n, m;
int desc_tag;
int desc_len;
int tmp;
int len2;
int len3;
int type;
for (n = 0; n < len; ) {
desc_tag = buffer[n];
desc_len = buffer[n + 1];
printf("sdt: tag=0x%x, len=0x%x\n", desc_tag, desc_len);
switch (desc_tag) {
case 0x48:
type = buffer[n + 2];
len2 = buffer[n + 3];
printf("type:0x%x, len2:0x%x\n", type, len2);
for (m = 0; m < len2; m++) {
printf("%c", buffer[n + m + 4]);
}
printf("\n");
if (!(service->provider)) {
service->provider = malloc(len2+1);
}
memcpy(service->provider, &buffer[n + 4], len2);
service->provider[len2] = 0;
len3 = buffer[n + 4 + len2];
printf("len3:0x%x\n", len3);
for (m = 0; m < len3; m++) {
printf("%c", buffer[n + m + 5 + len2]);
}
printf("\n");
if (!(service->name)) {
service->name = malloc(len3+1);
}
memcpy(service->name, &buffer[n + 5 + len2], len3);
service->name[len3] = 0;
break;
default:
printf("Unknown tag\n");
for (m = 0; m < desc_len; m++) {
printf("%02x", buffer[n + m + 2]);
}
printf("\n");
for (m = 0; m < desc_len; m++) {
tmp =buffer[n + m + 2];
if ((tmp > 31) && (tmp < 127)) {
printf("%c ", tmp);
} else {
printf(" ", tmp);
}
}
printf("\n");
break;
}
n += desc_len + 2;
}
}
static void parse_sdt_actual(struct demux_ts_s *this, int pid)
{
int program_count;
int service_count;
struct program_s *program;
uint8_t *buffer;
int section_length;
uint32_t table_id_ext;
uint32_t section_version_number;
uint32_t section_number;
uint32_t last_section_number;
uint32_t offset = 11;
uint32_t service_id;
int descriptors_loop_len;
int n;
program_count = this->pids[pid].program_count;
program = &(this->programs[program_count]);
buffer = this->pids[pid].section.buffer;
section_length = this->pids[pid].section.size;
// section_length = ((buffer[1] & 0x0f) << 8) | buffer[2];
table_id_ext = (buffer[3] << 8) | buffer[4];
section_version_number = (buffer[5] >> 1) & 0x1f;
section_number = buffer[6];
last_section_number = buffer[7];
for (n = offset; n < section_length - 4; ) {
service_id = (buffer[n] << 8) | buffer[n + 1];
descriptors_loop_len = ((buffer[n + 3] & 0x0f) << 8) | buffer[n + 4];
printf("sdt actual: serice_id:0x%x len:0x%x\n", service_id, descriptors_loop_len);
service_count = 0;
while ((this->services[service_count].program_id != INVALID_PROGRAM) &&
(this->services[service_count].program_id != service_id) &&
(service_count < 255 ) ) {
service_count++;
}
this->services[service_count].program_id = service_id;
process_sdt_descriptors( this, &(this->services[service_count]), &buffer[n + 5], descriptors_loop_len );
n += descriptors_loop_len + 5;
}
}
static void process_sdt(struct demux_ts_s *this, int pid)
{
struct program_s *program;
int section_length;
uint32_t crc32;
uint32_t calc_crc32;
uint8_t *buffer;
int n;
uint32_t reserved1;
uint32_t pcr_pid;
uint32_t reserved2;
uint32_t program_info_length;
uint32_t stream_type;
uint32_t elementary_pid;
uint32_t es_info_length;
uint32_t desc_tag;
uint32_t desc_len;
uint32_t ca_system_id;
uint32_t ca_pid;
uint32_t offset;
int program_count;
int i;
int m;
#ifdef TS_PMT_LOG
printf ("ts_demux: have all TS packets for the SDT section\n");
#endif
program_count = this->pids[pid].program_count;
program = &(this->programs[program_count]);
buffer = this->pids[pid].section.buffer;
section_length = this->pids[pid].section.size;
printf("buffer=%p\n", buffer);
i = 0;
for(n = 0; n < section_length + 3; n++) {
printf("%02x ", buffer[n]);
if ((n % 32) == 31) {
for (m = i; m < (i + 32); m++) {
if (buffer[m] > 31 && buffer[m] < 127) {
printf("%c", buffer[m]);
} else {
printf(".", buffer[m]);
}
}
i = n + 1;
printf("\n");
}
}
for (m = i; m < (n); m++) {
if (buffer[m] > 31 && buffer[m] < 127) {
printf("%c", buffer[m]);
} else {
printf(".", buffer[m]);
}
}
printf("\n");
crc32 = (uint32_t) buffer[section_length+3-4] << 24;
crc32 |= (uint32_t) buffer[section_length+3-3] << 16;
crc32 |= (uint32_t) buffer[section_length+3-2] << 8;
crc32 |= (uint32_t) buffer[section_length+3-1] ;
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32(this,
buffer,
section_length + 3 - 4, 0xffffffff);
if (crc32 != calc_crc32) {
printf ("demux_ts: demux error! SDT with invalid CRC32: packet_crc32: %#.8x calc_crc32: %#.8x\n",
crc32,calc_crc32);
return;
} else {
#ifdef TS_PMT_LOG
printf ("demux_ts: SDT CRC32 ok: %#.8x\n", crc32);
#endif
}
switch (buffer[0]) {
case 0x42:
/* Contains names of channels on this stream */
parse_sdt_actual(this, pid);
break;
default:
printf("demux_ts: Unknown SDT type\n");
}
}
/*
* NAME demux_ts_parse_pmt
*
* Parse a PMT. The PMT is expected to be exactly one section long,
* and that section is expected to be contained in a single TS packet.
*
* In other words, the PMT is assumed to describe a reasonable number of
* video, audio and other streams (with descriptors).
* FIXME: Implement support for multi section PMT.
*/
static void process_pmt(struct demux_ts_s *this, int pid)
{
struct program_s *program;
int section_length;
uint32_t crc32;
uint32_t calc_crc32;
uint8_t *buffer;
int n,m;
uint32_t reserved1;
uint32_t pcr_pid;
uint32_t reserved2;
uint32_t program_info_length;
uint32_t stream_type;
uint32_t elementary_pid;
uint32_t es_info_length;
uint32_t desc_tag;
uint32_t desc_len;
uint32_t ca_system_id;
uint32_t ca_pid;
uint32_t offset;
int program_count;
#ifdef TS_PMT_LOG
printf ("ts_demux: have all TS packets for the PMT section\n");
#endif
program_count = this->pids[pid].program_count;
program = &(this->programs[program_count]);
buffer = this->pids[pid].section.buffer;
section_length = this->pids[pid].section.size;
for(n = 0; n < section_length + 3; n++) {
printf("%02x ", buffer[n]);
if ((n % 32) == 31) {
printf("\n");
}
}
printf("\n");
crc32 = (uint32_t) buffer[section_length+3-4] << 24;
crc32 |= (uint32_t) buffer[section_length+3-3] << 16;
crc32 |= (uint32_t) buffer[section_length+3-2] << 8;
crc32 |= (uint32_t) buffer[section_length+3-1] ;
/* Check CRC. */
calc_crc32 = demux_ts_compute_crc32(this,
buffer,
section_length + 3 - 4, 0xffffffff);
if (crc32 != calc_crc32) {
printf ("demux_ts: demux error! PMT with invalid CRC32: packet_crc32: %#.8x calc_crc32: %#.8x\n",
crc32,calc_crc32);
return;
} else {
#ifdef TS_PMT_LOG
printf ("demux_ts: PMT CRC32 ok: %#.8x\n", crc32);
#endif
}
pcr_pid = (((uint32_t) buffer[8] << 8) | buffer[9]) & 0x1fff;
program_info_length = (((uint32_t) buffer[10] << 8) | buffer[11]) & 0x0fff;
printf (" pcr_pid: 0x%04x\n", pcr_pid);
printf (" program_info_length: 0x%04x\n", program_info_length);
/* Program info descriptor is currently just ignored. */
printf ("demux_ts: program_info_desc: ");
for (n = 0; n < program_info_length; n++)
printf ("%.2x ", buffer[12+n]);
printf ("\n");
offset = 12 + program_info_length;
for (offset = 12 + program_info_length; offset < section_length - 1; ) {
printf("offset = %d, section_length = %d\n", offset, section_length);
stream_type = buffer[offset];
elementary_pid = (((uint32_t) buffer[offset + 1] << 8) | buffer[offset + 2]) & 0x1fff;
es_info_length = (((uint32_t) buffer[offset + 3] << 8) | buffer[offset + 4]) & 0x0fff;
if (stream_type == 2) {
program->video.pid = elementary_pid;