-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbdmux.CPP
1152 lines (1099 loc) · 44.4 KB
/
bbdmux.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************
bbDMUX by Brent Beyeler, beyeler@home.com
*************************************************************************/
#include "bits.h"
#include <string.h>
#undef SHOW_RESYNCHS
#define MPEG_PROGRAM_END_CODE 0x000001B9
#define PACK_START_CODE 0x000001BA
#define SYSTEM_HEADER_START_CODE 0x000001BB
#define PACKET_START_CODE_PREFIX 0x000001
#define PROGRAM_ASSOCIATION_TABLE 0x0000
#define CONDITIONAL_ACCESS_TABLE 0x0001
#define NULL_PACKET 0x1FFF
#define TRANSPORT_SYNC_BYTE 0x47
#define PROGRAM_STREAM_MAP 0xBC
#define PRIVATE_STREAM_1 0xBD
#define PADDING_STREAM 0xBE
#define PRIVATE_STREAM_2 0xBF
#define ECM_STREAM 0xF0
#define EMM_STREAM 0xF1
#define PROGRAM_STREAM_DIRECTORY 0xFF
#define DSMCC_STREAM 0xF2
#define ITUTRECH222TYPEE_STREAM 0xF8
#define SUBSTREAM_AC3_0 0x80
#define SUBSTREAM_AC3_8 0x87
#define SUBSTREAM_DTS_0 0x88
#define SUBSTREAM_DTS_8 0x8F
#define SUBSTREAM_PCM_0 0xA0
#define SUBSTREAM_PCM_F 0xAF
#define SUBSTREAM_SUBPIC_0 0x20
#define SUBSTREAM_SUBPIC_1F 0x3F
#define BUFFER_SIZE 32768
static unsigned char buffer[BUFFER_SIZE];
static int bufidx;
static FILE *outfp;
static char outname[256];
static bool vob_flag;
static void transport_stream(char *fname, int argc, unsigned long PID_to_get);
void write_buffer()
{
if (fwrite(buffer, sizeof(unsigned char), bufidx + 1, outfp) != (unsigned int)bufidx + 1)
{
printf("Error writing to output file %s.", outname);
exit (1);
}
bufidx = -1;
}
void flush_buffer()
{
if (bufidx >= 0)
write_buffer();
}
int main(int argc, char* argv[])
{
unsigned long i, j, k, PES_packet_length, PES_header_data_length;
unsigned long PTS_DTS_flags, ESCR_flag, ES_rate_flag, DSM_trick_mode_flag;
unsigned long additional_copy_info_flag, PES_CRC_flag, PES_extension_flag;
unsigned long PES_private_data_flag, pack_header_field_flag;
unsigned long program_packet_sequence_counter_flag, PSTD_buffer_flag;
unsigned long PES_extension_flag_2, PES_extension_field_length;
unsigned long bytes_used, encrypted;
unsigned long substream_to_get, stream_to_get, stream_id, substream_id;
bool firsttime = true;
unsigned long stream_packets[256], stream_bytes[256], pack_packets;
unsigned long system_packets, mpeg_type;
unsigned long ac3_bytes[8], dts_bytes[8], pcm_bytes[16], subpic_bytes[32];;
unsigned long substream_packets[256], substream_bytes[256];
bool streams_found[256];
bool stream_encrypted[256];
bool substream_encrypted[256];
bool substreams_found[256];
bool mpeg2;
char tmpStr[256];
for (i = 0; i < 256; i++)
{
streams_found[i] = false;
stream_packets[i] = 0;
stream_bytes[i] = 0;
substreams_found[i] = false;
stream_encrypted[i] = false;
substream_encrypted[i] = false;
substream_packets[i] = 0;
substream_bytes[i] = 0;
}
for (i = 0; i < 8; i++)
{
ac3_bytes[i] = 0;
dts_bytes[i] = 0;
}
for (i = 0; i < 16; i++)
pcm_bytes[i] = 0;
for (i = 0; i < 32; i++)
subpic_bytes[i] = 0;
pack_packets = 0;
system_packets = 0;
outfp = NULL;
stream_to_get = -1;
substream_to_get = -1;
mpeg2 = false;
bufidx = -1;
printf("bbDMUX - version 1.9, by Brent Beyeler (beyeler@home.com)\n");
printf(" speed increases by, Apachez and Christian Vogelgsang\n\n");
if (argc < 2 || argc == 3)
{
printf("bbDUMX is an MPEG transport and program stream de-multiplexor\n\n");
printf("Usage: bbDMUX MPEG filename <stream id/PID saved name> <substream id>\n\n");
printf("Program stream examples:\n");
printf(" To get a list of the stream and substream id's present in an MPEG file\n");
printf(" bbDMUX testfile.mpg\n\n");
printf(" To save a video stream (id 0xe0) to the file video.m2v\n");
printf(" bbDMUX testfile.m2p 0xe0 video.m2v\n\n");
printf(" To save an audio stream (id 0xc0) to the file audio.m2a\n");
printf(" bbDMUX testfile.mpg 0xc0 audio.m2a\n\n");
printf(" To save an AC3 audio stream (substream 0x80) to the file audio.ac3\n");
printf(" bbDMUX testfile.vob 0xbd audio.ac3 0x80\n\n");
printf("Transport stream example:\n");
printf(" To save an video stream (PID = 0x001B) to the file video.m2v\n");
printf(" bbDMUX testfile.vob 0x001B video.m2v\n");
exit (1);
}
init_getbits(argv[1]);
strcpy(tmpStr, argv[1]);
strlwr(tmpStr);
vob_flag = (strstr(tmpStr, ".vob") != NULL);
mpeg_type = 0;
while (!mpeg_type)
{
if (look_ahead(32) == PACK_START_CODE)
mpeg_type = 1;
else
if (look_ahead(8) == TRANSPORT_SYNC_BYTE)
mpeg_type = 2;
else
getbits(8);
}
if (!mpeg_type)
{
printf("\nUnknown file type.\n");
exit(1);
}
if (argc > 2)
{
sscanf(argv[2], "0x%lx", &i);
if (mpeg_type == 1)
{
if (i > 0xFF)
{
printf("Stream ID must be in the range 0..0xFF\n");
exit (1);
}
}
else
{
if (i > 0x1FFF)
{
printf("PID must be in the range 0..0x1FFF\n");
exit (1);
}
}
stream_to_get = i;
if (argc > 3)
{
outfp = fopen(argv[3], "wb");
if (!outfp)
{
printf("Unable to open %s for output\n", argv[3]);
exit (1);
}
strcpy(outname, argv[3]);
}
else
{
printf("An output filename must be specified\n");
exit(1);
}
if (argc > 4)
{
sscanf(argv[4], "0x%lx", &i);
if (i > 0xFF)
{
printf("Substream ID must be in the range 0..0xFF\n");
exit (1);
}
substream_to_get = i;
vob_flag = true;
}
}
else
if (mpeg_type == 1)
printf("Scanning for stream id's, press control-c to quit ...\n");
else
printf("Scanning for PID's, press control-c to quit ...\n");
if (outfp)
{
if (mpeg_type == 1)
{
if ((vob_flag) && (stream_to_get == PRIVATE_STREAM_1) && (substream_to_get > -1))
printf("Saving stream 0x%02lX, substream 0x%02lX into file %s...\n", stream_to_get, substream_to_get, argv[3]);
else
printf("Saving stream 0x%02lX into file %s...\n", stream_to_get, argv[3]);
}
else
printf("Saving PID stream 0x%04lX into file %s...\n", stream_to_get, argv[3]);
}
if (mpeg_type == 2)
{
transport_stream(argv[1], argc, stream_to_get);
exit(0);
}
do
{
i = getbits(32);
//nextpass:
switch (i)
{
case PACK_START_CODE:
pack_packets++;
if (firsttime)
{
mpeg2 = look_ahead(2) == 1; // check if MPEG-1 or MPEG-2
if (mpeg2)
printf("\nFile %s is an MPEG-2 Program Stream\n\n", argv[1]);
else
printf("\nFile %s is an MPEG-1 Program Stream\n\n", argv[1]);
firsttime = false;
}
if (mpeg2)
{
getbits(32); // MPEG2 pack header
getbits(32);
getbits(13);
i = getbits(3);
for (j = 0; j < i; j++)
getbits(8); // stuffing bytes
}
else
{
getbits(32); // MPEG1 pack header
getbits(32);
}
break;
case SYSTEM_HEADER_START_CODE:
system_packets++;
getbits(32); // system header
getbits(32);
while (look_ahead(1) == 1)
getbits(24); // P-STD info
break;
case MPEG_PROGRAM_END_CODE:
break;
default:
if ((i >> 8) != PACKET_START_CODE_PREFIX)
// {
break;
//#ifdef SHOW_RESYNCHS
// if (argc > 3)
// {
// printf("\nUnknown bits in stream = 0x%08X at byte %.0f\n", i, bitcount() / 8 - 4);
// printf("\nAttempting resynch ... ");
// }
//#endif
// if (seek_sync(PACKET_START_CODE_PREFIX, 24))
// {
// i = 0x00000100 | getbits(8);
//#ifdef SHOW_RESYNCHS
// if (argc > 3)
// printf("succesfully resynched\n");
//#endif
// goto nextpass;
// }
//#ifdef SHOW_RESYNCHS
// if (argc > 3)
// printf("could not resynch\n");
// else
//#endif
// printf("\nUnknown bits in stream = 0x%08X at byte %.0f\n", i, bitcount() / 8 - 4);
// if (outfp)
// {
// flush_buffer();
// fclose(outfp);
// }
// exit(1);
// }
stream_id = i & 0x000000FF;
stream_packets[stream_id]++;
PES_packet_length = getbits(16);
encrypted = 0;
switch (stream_id)
{
case PROGRAM_STREAM_MAP:
case ECM_STREAM:
case EMM_STREAM:
case PROGRAM_STREAM_DIRECTORY:
case DSMCC_STREAM:
case ITUTRECH222TYPEE_STREAM:
case PRIVATE_STREAM_2:
case PADDING_STREAM:
if (stream_id == stream_to_get)
{
for (j = 0; j < PES_packet_length; j++)
{
buffer[++bufidx] = (unsigned char)getbits(8);
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
}
for (j = 0; j < PES_packet_length; j++)
getbits(8);
stream_bytes[stream_id] += j;
break;
default:
bytes_used = 0;
if (!mpeg2)
{
/* flush stuffing bytes */
while (look_ahead(8) == 0xff)
{
getbits(8);
bytes_used++;
}
if (look_ahead(2) == 1)
{
/* Get STD buffer size */
getbits(16);
bytes_used += 2;
}
if (look_ahead(4) == 2)
{
/* Get presentation time stamp and flag it as present in packet*/
getbits(32);
getbits(8);
bytes_used += 5;
}
else
if (look_ahead(4) == 3)
{
/* Get presentation time stamp and decoding time stamp */
getbits(32);
getbits(8);
getbits(32);
getbits(8);
bytes_used += 10;
}
else
{
if (getbits(8) != 0x0f)
{
printf("Invalid bits in MPEG-1 file %s.", argv[1]);
exit (1);
}
bytes_used++;
}
stream_bytes[stream_id] += PES_packet_length - bytes_used;
if (stream_id == stream_to_get)
{
for (j = 0; j < PES_packet_length - bytes_used; j++)
{
buffer[++bufidx] = (unsigned char)getbits(8);
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
}
else
for (j = 0; j < PES_packet_length - bytes_used; j++)
getbits(8);
}
else
{
getbits(2);
encrypted = getbits(2); // PES_scrambling_control
getbits(4);
PTS_DTS_flags = getbits(2);
ESCR_flag = get1bit();
ES_rate_flag = get1bit();
DSM_trick_mode_flag = get1bit();
additional_copy_info_flag = get1bit();
PES_CRC_flag = get1bit();
PES_extension_flag = get1bit();
PES_header_data_length = getbits(8);
if (PTS_DTS_flags > 1)
{
getbits(32); // PTS stamp
getbits(8);
bytes_used += 5;
}
if (PTS_DTS_flags > 2)
{
getbits(32); // DTS stamp
getbits(8);
bytes_used += 5;
}
if (ESCR_flag == 1)
{
getbits(32);
getbits(16);
bytes_used += 6;
}
if (ES_rate_flag == 1)
{
getbits(24);
bytes_used += 3;
}
if (DSM_trick_mode_flag == 1)
{
getbits(8);
bytes_used++;
}
if (additional_copy_info_flag == 1)
{
getbits(8);
bytes_used++;
}
if (PES_CRC_flag == 1)
{
getbits(16);
bytes_used += 2;
}
if (PES_extension_flag == 1)
{
PES_private_data_flag = get1bit();
pack_header_field_flag = get1bit();
program_packet_sequence_counter_flag = get1bit();
PSTD_buffer_flag = get1bit();
getbits(3);
PES_extension_flag_2 = get1bit();
bytes_used++;
if (PES_private_data_flag == 1)
{
for (j = 0; j < 128; j++)
getbits(8);
bytes_used += 128;
}
if (pack_header_field_flag == 1)
{
printf(" pack header field flag value not allowed in program streams\n");
exit(1);
}
if (program_packet_sequence_counter_flag == 1)
{
getbits(16);
bytes_used += 2;
}
if (PSTD_buffer_flag == 1)
{
getbits(16);
bytes_used += 2;
}
if (PES_extension_flag_2 == 1)
{
get1bit();
PES_extension_field_length = getbits(7);
bytes_used++;
for (j = 0; j < PES_extension_field_length; j++)
{
getbits(8);
bytes_used++;
}
}
}
for (j = 0; j < PES_header_data_length - bytes_used; j++)
getbits(8);
substream_id = getbits(8);
stream_bytes[stream_id] += PES_packet_length - PES_header_data_length - 3;
if ((stream_id == PRIVATE_STREAM_1) && (vob_flag))
{
substream_packets[substream_id]++;
j = PES_packet_length - PES_header_data_length - 3;
substream_bytes[substream_id] += j;
if ((substream_id >= SUBSTREAM_AC3_0) && (substream_id <= SUBSTREAM_AC3_8))
ac3_bytes[substream_id - SUBSTREAM_AC3_0] += j - 4;
if ((substream_id >= SUBSTREAM_DTS_0) && (substream_id <= SUBSTREAM_DTS_8))
dts_bytes[substream_id - SUBSTREAM_DTS_0] += j - 4;
if ((substream_id >= SUBSTREAM_PCM_0) && (substream_id <= SUBSTREAM_PCM_F))
pcm_bytes[substream_id - SUBSTREAM_PCM_0] += j - 7;
if ((substream_id >= SUBSTREAM_SUBPIC_0) && (substream_id <= SUBSTREAM_SUBPIC_1F))
subpic_bytes[substream_id - SUBSTREAM_SUBPIC_0] += j - 1;
}
if (stream_id == stream_to_get)
{
if ((stream_id == PRIVATE_STREAM_1) && (vob_flag) && (substream_to_get > -1))
{
if (substream_id == substream_to_get)
{
if ((substream_id >= SUBSTREAM_AC3_0) && (substream_id <= SUBSTREAM_AC3_8))
{
getbits(24); // discard the 3 AC3 info bytes
j = 4;
}
else
if ((substream_id >= SUBSTREAM_DTS_0) && (substream_id <= SUBSTREAM_DTS_8))
{
getbits(24); // discard the 3 DTS info bytes
j = 4;
}
else
if ((substream_id >= SUBSTREAM_PCM_0) && (substream_id <= SUBSTREAM_PCM_F))
{
getbits(24); // discard the 6 PCM info bytes
getbits(24);
j = 7;
}
else
if ((substream_id >= SUBSTREAM_SUBPIC_0) && (substream_id <= SUBSTREAM_SUBPIC_1F))
j = 1;
else
{
buffer[++bufidx] = (unsigned char)substream_id;
j = 1;
}
for (k = 0; k < PES_packet_length - PES_header_data_length - 3 - j; k++)
{
buffer[++bufidx] = (unsigned char)getbits(8);
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
}
else
for (j = 0; j < PES_packet_length - PES_header_data_length - 4; j++)
getbits(8);
}
else
{
buffer[++bufidx] = (unsigned char)substream_id;
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
for (j = 0; j < PES_packet_length - PES_header_data_length - 4; j++)
{
buffer[++bufidx] = (unsigned char)getbits(8);
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
}
}
else
for (j = 0; j < PES_packet_length - PES_header_data_length - 4; j++)
getbits(8);
}
break;
}
if (argc == 2)
{
if ((!streams_found[stream_id]) || ((vob_flag) && (stream_id == PRIVATE_STREAM_1) && (!substreams_found[substream_id])))
{
switch (stream_id)
{
case PROGRAM_STREAM_MAP:
printf("Found stream id 0x%02lX = Program Stream Map\n", stream_id);
break;
case PRIVATE_STREAM_1:
if (vob_flag)
{
if ((substream_id >= SUBSTREAM_AC3_0) && (substream_id <= SUBSTREAM_AC3_8))
printf("Found stream id 0x%02lX = Private Stream 1, substream 0x%02lX, AC3 Audio stream %lu\n", stream_id,
substream_id, substream_id - SUBSTREAM_AC3_0);
else
if ((substream_id >= SUBSTREAM_DTS_0) && (substream_id <= SUBSTREAM_DTS_8))
printf("Found stream id 0x%02lX = Private Stream 1, substream 0x%02lX, DTS Audio stream %lu\n", stream_id, substream_id, substream_id - SUBSTREAM_DTS_0);
else
if ((substream_id >= SUBSTREAM_PCM_0) && (substream_id <= SUBSTREAM_PCM_F))
printf("Found stream id 0x%02lX = Private Stream 1, substream 0x%02lX, PCM Audio stream %lu\n", stream_id, substream_id, substream_id - SUBSTREAM_PCM_0);
else
if ((substream_id >= SUBSTREAM_SUBPIC_0) && (substream_id <= SUBSTREAM_SUBPIC_1F))
printf("Found stream id 0x%02lX = Private Stream 1, substream 0x%02lX, Subpicture stream %lu\n", stream_id, substream_id, substream_id - SUBSTREAM_SUBPIC_0);
else
printf("Found stream id 0x%02lX = Private Stream 1, substream 0x%02lX\n", stream_id, substream_id);
substreams_found[substream_id] = true;
}
else
printf("Found stream id 0x%02lX = Private Stream 1\n", stream_id);
break;
case PRIVATE_STREAM_2:
printf("Found stream id 0x%02lX = Private Stream 2\n", stream_id);
break;
case ECM_STREAM:
printf("Found stream id 0x%02lX = ECM Stream\n", stream_id);
break;
case EMM_STREAM:
printf("Found stream id 0x%02lX = EMM Stream\n", stream_id);
break;
case PROGRAM_STREAM_DIRECTORY:
printf("Found stream id 0x%02lX = Program Stream Directory\n", stream_id);
break;
case DSMCC_STREAM:
printf("Found stream id 0x%02lX = DSMCC Stream\n", stream_id);
break;
case ITUTRECH222TYPEE_STREAM:
printf("Found stream id 0x%02lX = ITU-T Rec. H.222.1 type E\n", stream_id);
break;
case PADDING_STREAM:
printf("Found stream id 0x%02lX = Padding Stream\n", stream_id);
break;
default:
if ((stream_id >= 0xE0) && (stream_id <= 0xEF))
printf("Found stream id 0x%02lX = Video Stream %ld\n", stream_id, stream_id - 0xE0);
else
if ((stream_id >= 0xC0) && (stream_id <= 0xDF))
printf("Found stream id 0x%02lX = MPEG Audio Stream %ld\n", stream_id, stream_id - 0xC0);
else
printf("Found stream id 0x%02lX = other stream\n", stream_id);
}
streams_found[stream_id] = true;
}
if ((stream_id == PRIVATE_STREAM_1) && (vob_flag))
{
if (encrypted)
{
if (!substream_encrypted[substream_id])
{
printf("Stream id 0x%02lX, substream 0x%02lX is encrypted\n", stream_id, substream_id);
substream_encrypted[substream_id] = true;
}
}
}
else
{
if (encrypted)
{
if (!stream_encrypted[stream_id])
{
printf("Stream id 0x%02lX is encrypted\n", stream_id);
stream_encrypted[stream_id] = true;
}
}
}
}
}
} while ((i != MPEG_PROGRAM_END_CODE) && (!end_bs()));
printf("\nSummary:\n");
printf("\nMPEG Packs = %ld\n", pack_packets);
if (system_packets)
printf("System headers = %ld\n", system_packets);
for (i = 0; i < 256; i++)
{
if (stream_packets[i])
{
switch (i)
{
case PROGRAM_STREAM_MAP:
printf("Program Stream Map packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case PRIVATE_STREAM_1:
printf("Private Stream 1 packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
if (vob_flag)
{
for (j = 0; j < 256; j++)
{
if (substream_packets[j])
{
if ((j >= SUBSTREAM_AC3_0) && (j <= SUBSTREAM_AC3_8))
printf(" AC3 Audio stream %lu packets = %lu, total bytes = %lu\n", j - SUBSTREAM_AC3_0, substream_packets[j], ac3_bytes[j - SUBSTREAM_AC3_0]);
else
if ((j >= SUBSTREAM_DTS_0) && (j <= SUBSTREAM_DTS_8))
printf(" DTS Audio stream %lu packets = %lu, total bytes = %lu\n", j - SUBSTREAM_DTS_0, substream_packets[j], dts_bytes[j - SUBSTREAM_DTS_0]);
else
if ((j >= SUBSTREAM_PCM_0) && (j <= SUBSTREAM_PCM_F))
printf(" PCM Audio stream %lu packets = %lu, total bytes = %lu\n", j - SUBSTREAM_PCM_0, substream_packets[j], pcm_bytes[j - SUBSTREAM_PCM_0]);
else
if ((j >= SUBSTREAM_SUBPIC_0) && (j <= SUBSTREAM_SUBPIC_1F))
printf(" Subpicture stream %lu packets = %lu, total bytes = %lu\n", j - SUBSTREAM_SUBPIC_0, substream_packets[j], subpic_bytes[j - SUBSTREAM_SUBPIC_0]);
else
printf(" Substream 0x%02lX packets = %lu, total bytes = %lu\n", j, substream_packets[j], substream_bytes[j]);
}
}
}
break;
case PRIVATE_STREAM_2:
printf("Private Stream 2 packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case ECM_STREAM:
printf("ECM Stream packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case EMM_STREAM:
printf("EMM Stream packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case PROGRAM_STREAM_DIRECTORY:
printf("Program Stream Directory packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case DSMCC_STREAM:
printf("DSMCC Stream packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case ITUTRECH222TYPEE_STREAM:
printf("ITU-T Rec. H.222.1 type E packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
case PADDING_STREAM:
printf("Padding Stream packets = %lu, total bytes = %lu\n", stream_packets[i], stream_bytes[i]);
break;
default:
if ((i >= 0xE0) && (i <= 0xEF))
printf("Video stream %ld packets = %lu, total bytes = %lu\n", i - 0xE0, stream_packets[i], stream_bytes[i]);
else
if ((i >= 0xC0) && (i <= 0xDF))
printf("MPEG Audio stream %ld packets = %lu, total bytes = %lu\n", i - 0xC0, stream_packets[i], stream_bytes[i]);
else
printf("Other stream 0x%02lX packets = %lu, total bytes = %lu\n", i, stream_packets[i], stream_bytes[i]);
}
}
}
finish_getbits();
if (outfp)
{
flush_buffer();
fclose(outfp);
}
return (0);
}
struct stream_entry {
unsigned long stream_id;
unsigned long substream_id;
unsigned long stream_packets;
unsigned long stream_bytes;
int stream_encrypted;
int stream_found;
int pcm_bytes;
int subpic_bytes;
int ac3_bytes;
int dts_bytes;
};
void transport_stream(char *fname, int argc, unsigned long PID_to_get)
{
unsigned long i, j, k, PES_packet_length, PES_header_data_length;
unsigned long adaptation_bytes;
int payload_unit_start_indicator, PID;
bool firsttime = true;
unsigned long transport_packets, packet_data_bytes;
stream_entry *streams[8192];
for (i = 0; i < 8192; i++)
streams[i] = NULL;
transport_packets = 0;
bufidx = -1;
do
{
i = getbits(8);
//nextpass:
if (i == TRANSPORT_SYNC_BYTE)
{
transport_packets++;
if (firsttime)
{
printf("\nFile %s is an MPEG-2 Transport Stream\n\n", fname);
firsttime = false;
}
j = getbits(3);
payload_unit_start_indicator = (j & 0x2) >> 1;
PID = getbits(13);
if (streams[PID] == NULL)
{
streams[PID] = (stream_entry*)malloc(sizeof(stream_entry));
if (streams[PID] == NULL)
{
printf("Unable to allocate memory for stream entry.\n");
exit(1);
}
streams[PID]->stream_id = 0;
streams[PID]->substream_id = 0;
streams[PID]->stream_packets = 0;
streams[PID]->stream_bytes = 0;
streams[PID]->stream_encrypted = 0;
streams[PID]->stream_found = 0;
streams[PID]->pcm_bytes = 0;
streams[PID]->subpic_bytes = 0;
streams[PID]->ac3_bytes = 0;
streams[PID]->dts_bytes = 0;
}
streams[PID]->stream_packets++;
getbits(2);
i = getbits(2);
getbits(4);
if (i > 1)
{
j = getbits(8);
for (k = 0; k < j; k++)
getbits(8);
adaptation_bytes = j + 1;
}
else
adaptation_bytes = 0;
if ((i == 1) || (i == 3))
{
packet_data_bytes = 184 - adaptation_bytes;
if (packet_data_bytes > 0)
{
if ((look_ahead(24) == 0x000001) && payload_unit_start_indicator && (PID > 0x0F))
{
i = getbits(32);
packet_data_bytes -= 4;
if ((i >> 8) != PACKET_START_CODE_PREFIX)
{
printf("Packet start code not found.\n");
exit(1);
}
streams[PID]->stream_id = i & 0x000000FF;
PES_packet_length = getbits(16);
packet_data_bytes -= 2;
switch (streams[PID]->stream_id)
{
case PROGRAM_STREAM_MAP:
case ECM_STREAM:
case EMM_STREAM:
case PROGRAM_STREAM_DIRECTORY:
case DSMCC_STREAM:
case ITUTRECH222TYPEE_STREAM:
case PRIVATE_STREAM_2:
case PADDING_STREAM:
break;
default:
getbits(2);
streams[PID]->stream_encrypted = getbits(2); // PES_scrambling_control
getbits(12);
PES_header_data_length = getbits(8);
packet_data_bytes -= (3 + PES_header_data_length);
for (j = 0; j < PES_header_data_length; j++)
getbits(8);
streams[PID]->substream_id = getbits(8);
if ((streams[PID]->stream_id == PRIVATE_STREAM_1) && (vob_flag))
{
j = PES_packet_length - PES_header_data_length - 3;
if ((streams[PID]->substream_id >= SUBSTREAM_AC3_0) && (streams[PID]->substream_id <= SUBSTREAM_AC3_8))
streams[PID]->ac3_bytes += j - 4;
if ((streams[PID]->substream_id >= SUBSTREAM_DTS_0) && (streams[PID]->substream_id <= SUBSTREAM_DTS_8))
streams[PID]->dts_bytes += j - 4;
if ((streams[PID]->substream_id >= SUBSTREAM_PCM_0) && (streams[PID]->substream_id <= SUBSTREAM_PCM_F))
streams[PID]->pcm_bytes += j - 7;
if ((streams[PID]->substream_id >= SUBSTREAM_SUBPIC_0) && (streams[PID]->substream_id <= SUBSTREAM_SUBPIC_1F))
streams[PID]->subpic_bytes += j - 1;
}
if ((streams[PID]->stream_id == PRIVATE_STREAM_1) && (vob_flag))
{
if ((streams[PID]->substream_id >= SUBSTREAM_AC3_0) && (streams[PID]->substream_id <= SUBSTREAM_AC3_8))
{
getbits(24); // discard the 3 AC3 info bytes
j = 4;
}
else
if ((streams[PID]->substream_id >= SUBSTREAM_DTS_0) && (streams[PID]->substream_id <= SUBSTREAM_DTS_8))
{
getbits(24); // discard the 3 DTS info bytes
j = 4;
}
else
if ((streams[PID]->substream_id >= SUBSTREAM_PCM_0) && (streams[PID]->substream_id <= SUBSTREAM_PCM_F))
{
getbits(24); // discard the 6 PCM info bytes
getbits(24);
j = 7;
}
else
if ((streams[PID]->substream_id >= SUBSTREAM_SUBPIC_0) && (streams[PID]->substream_id <= SUBSTREAM_SUBPIC_1F))
j = 1;
else
{
if (PID == PID_to_get)
{
buffer[++bufidx] = (unsigned char)streams[PID]->substream_id;
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
j = 1;
streams[PID]->stream_bytes++;
}
packet_data_bytes -= j;
}
else
{
if (PID == PID_to_get)
{
buffer[++bufidx] = (unsigned char)streams[PID]->substream_id;
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
streams[PID]->stream_bytes++;
packet_data_bytes--;
}
break;
}
if (packet_data_bytes > 0)
{
if (PID == PID_to_get)
{
for (j = 0; j < packet_data_bytes; j++)
{
buffer[++bufidx] = (unsigned char)getbits(8);
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
}
else
{
for (j = 0; j < packet_data_bytes; j++)
getbits(8);
}
streams[PID]->stream_bytes += j;
}
}
else
{
if (PID == PID_to_get)
{
for (j = 0; j < packet_data_bytes; j++)
{
buffer[++bufidx] = (unsigned char)getbits(8);
if (bufidx == BUFFER_SIZE - 1)
write_buffer();
}
}
else
{
for (j = 0; j < packet_data_bytes; j++)
getbits(8);
}
streams[PID]->stream_bytes += j;
}
}
if ((argc == 2) && (!streams[PID]->stream_found))
{
switch (streams[PID]->stream_id)
{
case PROGRAM_STREAM_MAP:
printf("Found PID 0x%04X, stream id 0x%02lX = Program Stream Map\n", PID, streams[PID]->stream_id);
break;
case PRIVATE_STREAM_1:
if (vob_flag)
{
if ((streams[PID]->substream_id >= SUBSTREAM_AC3_0) && (streams[PID]->substream_id <= SUBSTREAM_AC3_8))
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 1, substream 0x%02lX, AC3 Audio stream %lu\n", PID, streams[PID]->stream_id, streams[PID]->substream_id, streams[PID]->substream_id - SUBSTREAM_AC3_0);
else
if ((streams[PID]->substream_id >= SUBSTREAM_DTS_0) && (streams[PID]->substream_id <= SUBSTREAM_DTS_8))
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 1, substream 0x%02lX, DTS Audio stream %lu\n", PID, streams[PID]->stream_id, streams[PID]->substream_id, streams[PID]->substream_id - SUBSTREAM_DTS_0);
else
if ((streams[PID]->substream_id >= SUBSTREAM_PCM_0) && (streams[PID]->substream_id <= SUBSTREAM_PCM_F))
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 1, substream 0x%02lX, PCM Audio stream %lu\n", PID, streams[PID]->stream_id, streams[PID]->substream_id, streams[PID]->substream_id - SUBSTREAM_PCM_0);
else
if ((streams[PID]->substream_id >= SUBSTREAM_SUBPIC_0) && (streams[PID]->substream_id <= SUBSTREAM_SUBPIC_1F))
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 1, substream 0x%02lX, Subpicture stream %lu\n", PID, streams[PID]->stream_id, streams[PID]->substream_id, streams[PID]->substream_id - SUBSTREAM_SUBPIC_0);
else
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 1, substream 0x%02lX\n", PID, streams[PID]->stream_id, streams[PID]->substream_id);
}
else
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 1\n", PID, streams[PID]->stream_id);
break;
case PRIVATE_STREAM_2:
printf("Found PID 0x%04X, stream id 0x%02lX = Private Stream 2\n", PID, streams[PID]->stream_id);
break;
case ECM_STREAM:
printf("Found PID 0x%04X, stream id 0x%02lX = ECM Stream\n", PID, streams[PID]->stream_id);
break;
case EMM_STREAM:
printf("Found PID 0x%04X, stream id 0x%02lX = EMM Stream\n", PID, streams[PID]->stream_id);
break;
case PROGRAM_STREAM_DIRECTORY: