-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtspatch.c
5064 lines (4387 loc) · 151 KB
/
tspatch.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
/* This is free and unencumbered software released into the public domain. */
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <libgen.h> /* for basename */
/***************/
/*** VERSION ***/
/***************/
/* Change it every commit */
/* Question: is it possible to retrieve git info ? */
#define TOOL_VERSION "1.00"
/*******************/
/*** COMPILATION ***/
/*******************/
#if 0
gcc -m32 tspatch.ts -o tspatch
#endif
/******************************/
/*** MACROS & TYPES: C CODE ***/
/******************************/
#define FALSE 0
#define TRUE 1
typedef unsigned bool_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
#define READ_32BITS(p) (((((uint32_t)(p)[0]) << 24) | (((uint32_t)(p)[1]) << 16) | (((uint32_t)(p)[2]) << 8) | ((uint32_t)(p)[3])) & 0xFFFFFFFF)
#define READ_24BITS(p) (( (((uint32_t)(p)[0]) << 16) | (((uint32_t)(p)[1]) << 8) | ((uint32_t)(p)[2])) & 0x00FFFFFF)
#define READ_16BITS(p) (( (((uint16_t)(p)[0]) << 8) | ((uint16_t)(p)[1])) & 0xFFFF)
#define READ_15BITS(p) (( (((uint16_t)(p)[0]) << 8) | ((uint16_t)(p)[1])) & 0x7FFF)
#define READ_13BITS(p) (( (((uint16_t)(p)[0]) << 8) | ((uint16_t)(p)[1])) & 0x1FFF)
#define READ_12BITS(p) (( (((uint16_t)(p)[0]) << 8) | ((uint16_t)(p)[1])) & 0x0FFF)
#define READ_08BITS(p) (( ((uint8_t) (p)[0])) & 0xFF)
#define READ_VERSION(p) ((((p)[0]) & 0x3E) >> 1)
#define WRITE_32BITS(p,v) (p)[0] = (uint8_t)(((v) >> 24) & 0xFF); (p)[1] = (uint8_t)(((v) >> 16) & 0xFF); (p)[2] = (uint8_t)(((v) >> 8) & 0xFF); (p)[3] = (uint8_t)((v) & 0xFF)
#define WRITE_24BITS(p,v) (p)[0] = (uint8_t)(((v) >> 16) & 0xFF); (p)[1] = (uint8_t)(((v) >> 8) & 0xFF); (p)[2] = (uint8_t)((v) & 0xFF)
#define WRITE_16BITS(p,v) (p)[0] = (uint8_t)(((v) >> 8) & 0xFF); (p)[1] = (uint8_t)((v) & 0xFF)
#define WRITE_13BITS(p,v) (p)[0] = (uint8_t)(((v) >> 8) & 0x1F); (p)[1] = (uint8_t)((v) & 0xFF)
#define WRITE_12BITS(p,v) (p)[0] = (uint8_t)(((v) >> 8) & 0x0F); (p)[1] = (uint8_t)((v) & 0xFF)
#define WRITE_08BITS(p,v) (p)[0] = (uint8_t)((v) & 0xFF)
#define WRITE_PID(p,v) (p)[0] = ((p)[0] & 0xE0) | (uint8_t)(((v) >> 8) & 0x1F); (p)[1] = (uint8_t)((v) & 0xFF)
#define WRITE_VERSION(p,v) (p)[0] = ((p)[0] & 0xC1) | (((v) & 0x1F) << 1)
/****************************/
/*** MACROS & TYPES: MPEG ***/
/****************************/
#define SMART_TABLE_SHOW
#define NB_MAX_ES_IN_PMT 20
#define PMT_INFO_MAX_SIZE 1
#define TS_SYNCHRO_BYTE (0x47)
#define TS_PACKET_SIZE (188)
#define INVALID_PID 0x1FFF
#define INVALID_TRANSPORT_STREAM_ID 0xFFFF
#define INVALID_PROGRAM_NUMBER 0xFFFF
#define INVALID_VERSION_NUMBER 0xFF
#define INVALID_BYTE 0xFF
#define PID_PAT (0x00)
#define PID_CAT (0x01)
#define PID_TSDT (0x02)
#define TID_PAT (0x00)
#define TID_CAT (0x01)
#define TID_PMT (0x02)
#define TID_DSMCC_LLCSNAP (0x0A)
#define TID_DSMCC_MESSAGE_HEADER (0x0B)
#define TID_DSMCC_DESCRIPTOR_LIST (0x0C)
#define STREAM_TYPE_11172_2_VIDEO (0x01)
#define STREAM_TYPE_13818_2_VIDEO (0x02)
#define STREAM_TYPE_11172_3_AUDIO (0x03)
#define STREAM_TYPE_13818_3_AUDIO (0x04)
#define STREAM_TYPE_13818_1_PRIVATE_SECTIONS (0x05)
#define STREAM_TYPE_13818_1_PES_PACKETS_WITH_PRIVATE_DATA (0x06)
#define STREAM_TYPE_13522_MHEG (0x07)
#define STREAM_TYPE_DSMCC (0x08)
#define STREAM_TYPE_H222_1 (0x09)
#define STREAM_TYPE_13818_6_TYPE_A (0x0A)
#define STREAM_TYPE_13818_6_TYPE_B (0x0B)
#define STREAM_TYPE_13818_6_TYPE_C (0x0C)
#define STREAM_TYPE_13818_6_TYPE_D (0x0D)
#define STREAM_TYPE_13818_1_AUXILIARY (0x0E)
#define STREAM_TYPE_AUDIO_ADTS (0x0F)
#define STREAM_TYPE_AUDIO_LATM (0x11)
#define STREAM_TYPE_H264 (0x1B)
#define DESCRIPTOR_TAG_VIDEO_STREAM (0x02)
#define DESCRIPTOR_TAG_AUDIO_STREAM (0x03)
#define DESCRIPTOR_TAG_HIERARCHY (0x04)
#define DESCRIPTOR_TAG_REGISTRATION (0x05)
#define DESCRIPTOR_TAG_DATA_STREAM_ALIGNMENT (0x06)
#define DESCRIPTOR_TAG_TARGET_BACKGROUND_GRID (0x07)
#define DESCRIPTOR_TAG_VIDEO_WINDOW (0x08)
#define DESCRIPTOR_TAG_CA (0x09)
#define DESCRIPTOR_TAG_ISO_639_LANGUAGE (0x0A)
#define DESCRIPTOR_TAG_SYSTEM_CLOCK (0x0B)
#define DESCRIPTOR_TAG_MULTIPLEX_BUFFER_UTILIZATION (0x0C)
#define DESCRIPTOR_TAG_COPYRIGHT (0x0D)
#define DESCRIPTOR_TAG_MAXIMUM_BITRATE (0x0E)
#define DESCRIPTOR_TAG_PRIVATE_DATA_INDICATOR (0x0F)
#define DESCRIPTOR_TAG_SMOOTHING_BUFFER (0x10)
#define DESCRIPTOR_TAG_STD (0x11)
#define DESCRIPTOR_TAG_IBP (0x12)
#define DESCRIPTOR_TAG_CAROUSEL_IDENTIFIER (0x13)
#define DESCRIPTOR_TAG_ASSOCIATION_TAG (0x14)
#define DESCRIPTOR_TAG_DEFERRED_ASSOCIATION_TAGS (0x15)
#define DESCRIPTOR_TAG_NPT_REFERENCE (0x17)
#define DESCRIPTOR_TAG_NPT_ENDPOINT (0x18)
#define DESCRIPTOR_TAG_STREAM_MODE (0x19)
#define DESCRIPTOR_TAG_STREAM_EVENT (0x1A)
#define DESCRIPTOR_TAG_MPEG4_VIDEO (0x1B)
#define DESCRIPTOR_TAG_MPEG4_AUDIO (0x1C)
#define DESCRIPTOR_TAG_IOD (0x1D)
#define DESCRIPTOR_TAG_SL (0x1E)
#define DESCRIPTOR_TAG_FMC (0x1F)
#define DESCRIPTOR_TAG_EXTERNAL_ES_ID (0x20)
#define DESCRIPTOR_TAG_MUXCODE (0x21)
#define DESCRIPTOR_TAG_FMXBUFFERISZE (0x22)
#define DESCRIPTOR_TAG_MULTIPLEXBUFFER (0x23)
#define DESCRIPTOR_TAG_CONTENT_LABELLING (0x24)
#define DESCRIPTOR_TAG_METADATA_POINTER (0x25)
#define DESCRIPTOR_TAG_METADATA (0x26)
#define DESCRIPTOR_TAG_METADATA_STD (0x27)
#define DESCRIPTOR_TAG_AVC_VIDEO (0x28)
#define DESCRIPTOR_TAG_IPMP (0x29)
#define DESCRIPTOR_TAG_AVC_TIMING_AND_HRD (0x2A)
#define DESCRIPTOR_TAG_MPEG2_AAC_AUDIO (0x2B)
#define DESCRIPTOR_TAG_FLEXMUXTIMINIG (0x2C)
#define DESCRIPTOR_TAG_MPEG4_TEXT (0x2D)
#define DESCRIPTOR_TAG_MPEG4_AUDIO_EXTENSION (0x2E)
#define DESCRIPTOR_TAG_AUXILIARY_VIDEO_STREAM (0x2F)
#define DESCRIPTOR_TAG_SVC_EXTENSION (0x30)
#define DESCRIPTOR_TAG_MVC_EXTENSION (0x31)
#define DESCRIPTOR_TAG_J2K_VIDEO (0x32)
#define DESCRIPTOR_TAG_MVC_OPERATION_POINT (0x33)
#define DESCRIPTOR_TAG_MPEG2_STEREOSCOPIC_VIDEO_FORMAT (0x34)
#define DESCRIPTOR_TAG_STEREOSCOPIC_PROGRAM_INFO (0x35)
#define DESCRIPTOR_TAG_STEREOSCOPIC_VIDEO_INFO (0x36)
#define DESCRIPTOR_TAG_TRANSPORT_PROFILE (0x37)
#define DESCRIPTOR_TAG_HEVC_VIDEO (0x38)
#define SECTION_TABLE_ID_SIZE 1
#define SECTION_SECTION_LENGTH_SIZE 2
#define SECTION_TRANSPORT_STREAM_ID_SIZE 2
#define SECTION_VERSION_NUMBER_SIZE 1
#define SECTION_SECTION_NUMBER_SIZE 1
#define SECTION_LAST_SECTION_NUMBER_SIZE 1
#define SECTION_PID_SIZE 2
#define SECTION_CRC_SIZE 4
#define SECTION_HEADER_SIZE (SECTION_TABLE_ID_SIZE + SECTION_SECTION_LENGTH_SIZE)
#define TS_PCR_BASE_CLOCK (90000)
typedef struct
{
uint32_t base_32lb;
uint16_t extension;
uint8_t base_1hb;
} pcr_t;
typedef struct
{
uint32_t count;
pcr_t pcr;
} packet_time_t;
typedef struct
{
uint16_t pid;
uint16_t tid;
} packet_info_t;
typedef struct
{
uint16_t section_length;
uint16_t transport_stream_id;
uint8_t version_number;
uint8_t section_number;
uint8_t last_section_number;
uint8_t number_of_programs;
} pat_header_t;
typedef struct
{
uint16_t section_length;
uint16_t program_number;
uint16_t pcr_pid;
uint16_t program_info_length;
uint8_t version_number;
uint8_t section_number;
uint8_t last_section_number;
uint8_t number_of_es;
} pmt_header_t;
typedef struct
{
uint16_t pid;
uint16_t info_length;
uint8_t stream_type;
uint8_t info[PMT_INFO_MAX_SIZE];
} pmt_es_t;
typedef struct
{
uint16_t tsid;
uint16_t number;
uint16_t pmt_pid;
uint16_t pcr_pid;
uint16_t audio_pid;
uint16_t video_pid;
uint16_t dsmcc_pid;
uint16_t es_pid;
} prog_t;
static void init_prog(prog_t *prog)
{
prog->tsid = INVALID_TRANSPORT_STREAM_ID;
prog->number = INVALID_PROGRAM_NUMBER;
prog->pmt_pid = INVALID_PID;
prog->pcr_pid = INVALID_PID;
prog->audio_pid = INVALID_PID;
prog->video_pid = INVALID_PID;
prog->dsmcc_pid = INVALID_PID;
prog->es_pid = INVALID_PID;
}
/***************************/
/*** MACROS & TYPES: DVB ***/
/***************************/
#define PID_NIT (0x10)
#define PID_SDT (0x11)
#define PID_BAT (0x11)
#define PID_EIT (0x12)
#define PID_RST (0x12)
#define PID_TDT (0x14)
#define PID_TOT (0x14)
#define TID_NIT_ACTUAL (0x40)
#define TID_NIT_OTHER (0x41)
#define TID_SDT_ACTUAL (0x42)
#define TID_SDT_OTHER (0x46)
#define TID_BAT (0x4A)
#define TID_EITPF_ACTUAL (0x4E)
#define TID_EITPF_OTHER (0x4F)
#define TID_EIT_SCHEDULE_ACTUAL_MIN (0x50)
#define TID_EIT_SCHEDULE_ACTUAL_MAX (0x5F)
#define TID_EIT_SCHEDULE_OTHER_MIN (0x60)
#define TID_EIT_SCHEDULE_OTHER_MAX (0x6F)
#define TID_TDT (0x70)
#define TID_RST (0x71)
#define TID_ST (0x72)
#define TID_TOT (0x73)
#define TID_AIT (0x74)
#define TID_DSMCC_DVB_LLCSNAP (0x3A)
#define TID_DSMCC_DVB_USER_NETWORK_MESSAGE (0x3B)
#define TID_DSMCC_DVB_DOWNLOAD_DATA_MESSAGE (0x3C)
#define TID_DSMCC_DVB_DESCRIPTOR_LIST (0x3D)
#define TID_DSMCC_DVB_PRIVATE_DATA (0x3E)
#define DESCRIPTOR_TAG_NETWORK_NAME (0x40)
#define DESCRIPTOR_TAG_SERVICE_LIST (0x41)
#define DESCRIPTOR_TAG_STUFFING (0x42)
#define DESCRIPTOR_TAG_SATELLITE_DELIVERY_SYSTEM (0x43)
#define DESCRIPTOR_TAG_CABLE_DELIVERY_SYSTEM (0x44)
#define DESCRIPTOR_TAG_VBI_DATA (0x45)
#define DESCRIPTOR_TAG_VBI_TELETEXT (0x46)
#define DESCRIPTOR_TAG_BOUQUET_NAME (0x47)
#define DESCRIPTOR_TAG_SERVICE (0x48)
#define DESCRIPTOR_TAG_COUNTRY_AVAILABILITY (0x49)
#define DESCRIPTOR_TAG_LINKAGE (0x4A)
#define DESCRIPTOR_TAG_NVOD_REFERENCE (0x4B)
#define DESCRIPTOR_TAG_TIME_SHIFTED_SERVICE (0x4C)
#define DESCRIPTOR_TAG_SHORT_EVENT (0x4D)
#define DESCRIPTOR_TAG_EXTENDED_EVENT (0x4E)
#define DESCRIPTOR_TAG_TIME_SHIFTED_EVENT (0x4F)
#define DESCRIPTOR_TAG_COMPONENT (0x50)
#define DESCRIPTOR_TAG_MOSAIC (0x51)
#define DESCRIPTOR_TAG_STREAM_IDENTIFIER (0x52)
#define DESCRIPTOR_TAG_CA_IDENTIFIER (0x53)
#define DESCRIPTOR_TAG_CONTENT (0x54)
#define DESCRIPTOR_TAG_PARENTAL_RATING (0x55)
#define DESCRIPTOR_TAG_TELETEXT (0x56)
#define DESCRIPTOR_TAG_TELEPHONE (0x57)
#define DESCRIPTOR_TAG_LOCAL_TIME_OFFSET (0x58)
#define DESCRIPTOR_TAG_SUBTITLING (0x59)
#define DESCRIPTOR_TAG_TERRESTRIAL_DELIVERY_SYSTEM (0x5A)
#define DESCRIPTOR_TAG_MULTILINGUAL_NETWORK_NAME (0x5B)
#define DESCRIPTOR_TAG_MULTILINGUAL_BOUQUET_NAME (0x5C)
#define DESCRIPTOR_TAG_MULTILINGUAL_SERVICE_NAME (0x5D)
#define DESCRIPTOR_TAG_MULTILINGUAL_COMPONENT (0x5E)
#define DESCRIPTOR_TAG_PRIVATE_DATA_SPECIFIER (0x5F)
#define DESCRIPTOR_TAG_SERVICE_MOVE (0x60)
#define DESCRIPTOR_TAG_SHORT_SMOOTHING_BUFFER (0x61)
#define DESCRIPTOR_TAG_FREQUENCY_LIST (0x62)
#define DESCRIPTOR_TAG_PARTIAL_TRANSPORT_STREAM (0x63)
#define DESCRIPTOR_TAG_DATA_BROADCAST (0x64)
#define DESCRIPTOR_TAG_SCRAMBLING (0x65)
#define DESCRIPTOR_TAG_DATA_BROADCAST_ID (0x66)
#define DESCRIPTOR_TAG_TRANSPORT_STREAM (0x67)
#define DESCRIPTOR_TAG_APPLICATION_SIGNALLING (0x6F)
#define DESCRIPTOR_TAG_ADAPTATION_FIELD_DATA (0x70)
#define DESCRIPTOR_TAG_SERVICE_IDENTIFIER (0x71)
#define DESCRIPTOR_TAG_SERVICE_AVAILABILITY (0x72)
#define DESCRIPTOR_TAG_DEFAULT_AUTHORITY (0x73)
#define DESCRIPTOR_TAG_RELATED_CONTENT (0x74)
#define DESCRIPTOR_TAG_TVA_ID (0x75)
#define DESCRIPTOR_TAG_CONTENT_IDENTIFIER (0x76)
#define DESCRIPTOR_TAG_TIME_SLICE_FEC_IDENTIFIER (0x77)
#define DESCRIPTOR_TAG_ECM_REPETITION_RATE (0x78)
#define DESCRIPTOR_TAG_S2_SATELLITE_DELIVERY_SYSTEM (0x79)
#define DESCRIPTOR_TAG_ENHANCED_AC3 (0x7A)
#define DESCRIPTOR_TAG_DTS_AUDIO_STREAM (0x7B)
#define DESCRIPTOR_TAG_AAC (0x7C)
#define DESCRIPTOR_TAG_XAIT_LOCATION (0x7D)
#define DESCRIPTOR_TAG_FTA_CONTENT_MANAGEMENT (0x7E)
#define DESCRIPTOR_TAG_EXTENSION (0x7F)
typedef struct
{
uint16_t section_length;
uint16_t transport_stream_id;
uint16_t original_network_id;
uint8_t version_number;
uint8_t section_number;
uint8_t last_section_number;
uint8_t number_of_services;
} sdt_header_t;
typedef struct
{
const char *base;
const char *file;
} ait_url_t;
typedef struct
{
uint16_t id;
char name[64];
} service_t;
static void init_service(service_t *service)
{
service->id = 0;
service->name[0] = 0;
}
typedef struct
{
ait_url_t url;
uint16_t pid;
uint16_t id;
uint8_t tpl;
} ait_t;
static void init_ait(ait_t *ait)
{
ait->url.base = NULL;
ait->url.file = NULL;
ait->pid = INVALID_PID;
ait->id = 0;
ait->tpl = INVALID_BYTE;
}
/*************/
/*** PRINT ***/
/*************/
/*
** TODO: review, fix and improve trace handling (this is very prehistoric)
**
** really, review log levels, it's a real mess
**
**
** also, there's a question: what is an error ?
** - if error when parsing, then it's not a proper error,
** it's an error of TS file format, to be reported to user as information
** - if error when calling system primitive, then it's a real error
** need to review all error messages
**
** at last, review assert and exit usages
**
** oh, another 'at last': harmonize comments (C style or C++)
** first idea was to reserve C++ style comments for code that is disabled,
** but kept to show an alternative implementation
** while C style comments were real comments
** but C++ style comments are more convenient, so usage has been deviant
*/
#define print_output printf // TODO: remove me
#define PRINT_LEVEL_0 (0) // must print, always, whatever the weather is
#define PRINT_LEVEL_1 (1) // more info
#define PRINT_LEVEL_2 (2) // more details, risk to be flooded
#define PRINT_LEVEL_3 (3) // reserved for the debug of the code
static unsigned print_level = 0;
#define print0 printf
#define print1 (print_level >= PRINT_LEVEL_1) && printf
#define print2 (print_level >= PRINT_LEVEL_2) && printf
#define print3 (print_level >= PRINT_LEVEL_3) && printf
static char get_printable_character(uint8_t c)
{
if (0x20 <= c && c < 0x7F)
{
return c;
}
return '.';
}
static void print_string(const char *header,
const uint8_t *buffer,
unsigned size,
unsigned level)
{
unsigned i;
if (print_level >= level)
{
if (header != NULL)
{
print0("%s", header);
}
for (i = 0; i < size; i++)
{
print0("%c", get_printable_character(buffer[i]));
}
print0("\n");
}
}
static void print_buffer(const char *header,
const uint8_t *buffer,
unsigned size,
unsigned level)
{
unsigned i;
if (print_level >= level)
{
if (header != NULL)
{
print0("%s", header);
}
for (i = 0; i < size; i++)
{
print0("%02X ", buffer[i]);
}
print0("\n");
}
}
static void dump_buffer(const char *comment,
const uint8_t *buffer,
unsigned size,
unsigned level)
{
const char *space;
const char *space0 = "";
const char *space1 = " ";
const char *space2 = " ";
const unsigned step = 16;
unsigned i, j;
if (print_level >= level)
{
print0("DUMP BUFFER (size = %u): %s\n",
size, comment == NULL ? "" : comment);
for (i = 0; i < size; i += step)
{
print0(" 0x%04X:", i);
for (j= 0; j < step; j++)
{
space = (j == step / 2) ? space2 : space1;
if ((i + j) < size)
{
print0("%s%02X", space, buffer[i + j]);
}
else
{
print0("%s ", space);
}
}
print0(" ");
for (j= 0; (j < step) && ((i + j) < size); j++)
{
space = (j == step / 2) ? space1 : space0;
print0("%s%c", space, get_printable_character(buffer[i + j]));
}
print0("\n");
}
print0("\n");
}
}
/************/
/*** CODE ***/
/************/
/*
** Misc tools
*/
static void close_file(FILE *file)
{
if (NULL != file)
{
fflush(file);
fclose(file);
}
}
static uint32_t get_file_size(const char *filename)
{
FILE *file;
long int size;
int ret;
file = fopen(filename, "rb");
if (file == NULL)
{
print0("cannot open %s\n", filename);
return 0;
}
ret = fseek(file, 0, SEEK_END);
if (0 != ret)
{
print0("ERROR: cannot seek in %s\n", filename); // system error
return 0;
}
size = ftell(file);
if (size < 0)
{
print0("ERROR: cannot tell in %s\n", filename); // system error
return 0;
}
return size;
}
/*
** Packet level tools
*/
static bool_t check_crc32(const uint8_t *buffer, unsigned length);
static uint16_t get_pid(const uint8_t *packet)
{
if (TS_SYNCHRO_BYTE != packet[0])
{
print0("lost synchro, got 0x%02X synchro byte instead of 0x%02X\n",
packet[0], TS_SYNCHRO_BYTE);
return INVALID_PID;
}
return READ_13BITS(&packet[1]);
}
static bool_t set_pid(uint8_t *packet, uint16_t pid)
{
if (INVALID_PID != pid)
{
packet[1] = (uint8_t)((0x1F00 & pid) >> 8) | (0xE0 & packet[1]);
packet[2] = (uint8_t)((0x00FF & pid) >> 0);
// TODO: change WRITE_13BITS macro to keep the 3 first bits
return TRUE;
}
return FALSE;
}
static bool_t toggle_tsc_bits(uint8_t *packet)
{
static int print_done = 0;
uint8_t old_tsc;
uint8_t new_tsc;
old_tsc = packet[3] & 0xC0;
new_tsc = (0x00 == old_tsc) ? 0x80 : 0x00;
if (0 == print_done)
{
print1("toggle TSC bits from %c%c to %c%c\n",
(old_tsc & 0x80) ? '1' : '0',
(old_tsc & 0x40) ? '1' : '0',
(new_tsc & 0x80) ? '1' : '0',
(new_tsc & 0x40) ? '1' : '0');
print_done = 1;
}
packet[3] &= 0x3F;
packet[3] |= new_tsc;
return TRUE;
}
static bool_t read_pcr(const uint8_t *packet, pcr_t *pcr)
{
const
uint8_t *adaptation_field;
uint8_t adaptation_field_control;
/* Read the adaptation field control word */
adaptation_field_control = (packet[3] >> 4) & 0x03;
if (0 == (adaptation_field_control & 0x02))
{
/* The packet contains no adaptation field */
return FALSE;
}
/* Point out the adaptation field */
adaptation_field = packet + 4;
/* Check the adaptation field length */
if (0 == adaptation_field[0])
{
/* The packet contains no adaptation field */
return FALSE;
}
/* Check the PCR flag */
if (0 == (adaptation_field[1] & 0x10))
{
/* Adaptation field with no PCR inside */
return FALSE;
}
/* Read the PCR */
pcr->base_1hb = (adaptation_field[2] & 0x80) >> 7;
pcr->base_32lb = ((uint32_t)(adaptation_field[2] & 0x7F)) << 25
| ((uint32_t)(adaptation_field[3] & 0xFF)) << 17
| ((uint32_t)(adaptation_field[4] & 0xFF)) << 9
| ((uint32_t)(adaptation_field[5] & 0xFF)) << 1
| ((uint32_t)(adaptation_field[6] & 0x80)) >> 7;
pcr->extension = ((uint16_t)(adaptation_field[7] & 0x01)) << 8
| ((uint16_t)(adaptation_field[8] & 0xFF)) << 0;
return TRUE;
}
static const char *get_section_name(uint16_t pid, uint8_t table_id);
static uint8_t *get_section(const uint8_t *packet, bool_t try)
{
uint16_t pid;
uint16_t section_length;
uint8_t transport_error_indicator;
uint8_t payload_unit_start_indicator;
uint8_t transport_priority;
uint8_t transport_scrambling_control;
uint8_t adaptation_field_control;
uint8_t adaptation_field_length;
uint8_t continuity_counter;
uint8_t pointer_field;
uint8_t section_syntax_indicator;
uint8_t private_indicator;
pid = READ_13BITS(&packet[1]);
transport_error_indicator = (packet[1] >> 7) & 0x01;
payload_unit_start_indicator = (packet[1] >> 6) & 0x01;
transport_priority = (packet[1] >> 5) & 0x01;
adaptation_field_control = (packet[3] >> 4) & 0x03;
continuity_counter = (packet[3] >> 0) & 0x03;
/* Go after 4-bytes header */
packet += 4;
if (0 == (adaptation_field_control & 0x02))
{
adaptation_field_length = 0;
}
else
{
adaptation_field_length = 1 + packet[0]; /* +1 for own adaptation_field_length size */
}
if (adaptation_field_length > (TS_PACKET_SIZE - 4))
{
if (try)
{
print1("invalid adaptation field (len = %u) on pid 0x%X (%u)\n",
adaptation_field_length, pid, pid);
}
else
{
print0("invalid adaptation field (len = %u) on pid 0x%X (%u)\n",
adaptation_field_length, pid, pid);
}
return NULL;
}
/* Go to payload */
packet += adaptation_field_length;
if (payload_unit_start_indicator == 0)
{
print1("not a section start on pid 0x%X (%u)\n",
pid, pid);
return (uint8_t*)packet;
}
pointer_field = 1 + packet[0]; /* +1 for own pointer_field size */
if (pointer_field > (TS_PACKET_SIZE - 4))
{
if (try)
{
print1("invalid pointer field (%u) on pid 0x%X (%u)\n",
pointer_field, pid, pid);
}
else
{
print0("invalid pointer field (%u) on pid 0x%X (%u)\n",
pid, pid, pointer_field);
}
return NULL;
}
/* Go to section */
packet += pointer_field;
section_syntax_indicator = (packet[1] >> 7) & 0x01;
private_indicator = (packet[1] >> 6) & 0x01;
section_length = READ_12BITS(&packet[1]);
print3("pid = 0X%X (%u), section_syntax_indicator = 0x%X, private_indicator = 0x%X, section_length = %u (try = %u)\n",
pid, pid, section_syntax_indicator, private_indicator, section_length, try);
dump_buffer("debug", packet, 16, PRINT_LEVEL_3);
// The maximum number of bytes in a section of a ITU-T Rec. H.222.0 | ISO/IEC 13818-1 defined PSI table is 1024 bytes.
// The maximum number of bytes in a private_section is 4096 bytes.
if (private_indicator == 0)
{
if (section_length > 1021)
{
if (try)
{
print1("invalid section length (%u) on pid 0x%X (%u)\n",
section_length, pid, pid);
}
else
{
print0("invalid section length (%u) on pid 0x%X (%u)\n",
section_length, pid, pid);
}
return NULL;
}
}
else if (private_indicator == 1)
{
if (section_length > 4093)
{
if (try)
{
print1("invalid private section length (%u) on pid 0x%X (%u)\n",
section_length, pid, pid);
}
else
{
print0("invalid private section length (%u) on pid 0x%X (%u)\n",
section_length, pid, pid);
}
return NULL;
}
}
dump_buffer(get_section_name(pid, packet[0]), packet, section_length + 3, PRINT_LEVEL_2);
if (try)
{
return (uint8_t*)packet;
}
section_length += SECTION_HEADER_SIZE;
section_length -= SECTION_CRC_SIZE;
if (pid == PID_TDT && packet[0] == TID_TDT)
{
/* This function pertains to MPEG packet level, */
/* but DVB table id is tested, this is an heresy */
print2("TDT section has no CRC\n");
}
else if (section_syntax_indicator == 0)
{
/* When section_syntax_indicator is 0 */
/* then private_indicator should be 1 */
/* and CRC_32 should be 0xFFFFFFFF */
if (private_indicator == 1)
{
print2("dont check CRC when syntax indicator is 0\n");
// TODO: check that CRC is 0xFFFFFFFF
}
else if (try)
{
print1("got section_syntax_indicator (%u) and private_indicator (%u)\n",
section_syntax_indicator, private_indicator);
}
else
{
print0("section_syntax_indicator (%u) and private_indicator (%u) are not coherent\n",
section_syntax_indicator, private_indicator);
}
}
else if (! check_crc32(packet, (unsigned)section_length))
{
print0("CRC error on TID 0x%X (%u)\n",
packet[0], packet[0]);
return NULL;
}
return (uint8_t*)packet;
}
static bool_t is_pid_in_list(uint16_t pid,
uint16_t extended_table_id,
packet_info_t *list,
uint32_t size)
{
uint32_t i;
for (i = 0; i < size; i++)
{
if (pid == list[i].pid)
{
if (extended_table_id == list[i].tid)
{
return TRUE;
}
if (0xFFFF == extended_table_id || 0xFFFF == list[i].tid)
{
list[i].tid = 0xFFFF;
return TRUE;
}
}
}
return FALSE;
}
static void show_pid_list(packet_info_t *list, uint32_t size)
{
packet_info_t temp;
uint16_t pid_min;
uint32_t nb_pids;
uint32_t i;
uint32_t j;
uint32_t j_min;
if (0 == size)
{
print_output("no PID found !\n");
return;
}
nb_pids = 1;
/*
** Sort the list in PID order
*/
/*
** TODO: fix doubles
*/
for (i = 0; i < size; i++)
{
pid_min = 0xFFFF;
for (j = i; j < size; j++)
{
if (list[j].pid < pid_min)
{
pid_min = list[j].pid;
j_min = j;
}
}
temp = list[i];
list[i] = list[j_min];
list[j_min] = temp;
if (i > 0 && list[i].pid != list[i-1].pid)
{
nb_pids++;
}
}
/*
** Show the list
*/
print_output("found %lu different PIDs\n", nb_pids);
for (i = 0; i < size; i++)
{
if (list[i].pid < 0x20)
{
print_output(" - 0x%04X (%u) - %s\n",
list[i].pid, list[i].pid,
get_section_name(list[i].pid, list[i].tid));
}
else if (0xFFFF == list[i].tid)
{
print_output(" - 0x%04X (%u) - PES\n",
list[i].pid, list[i].pid);
}
else
{
print_output(" - 0x%04X (%u) - PES or SECTION with table_id 0x%02X (%u)\n",
list[i].pid, list[i].pid,
list[i].tid, list[i].tid);
}
}
/*
** TODO: for each PID, add number of packets
*/
}
static uint32_t pcr1_minus_pcr2(const pcr_t *pcr1, const pcr_t *pcr2)
{
uint32_t pcr_diff;
if (pcr1->base_1hb == pcr2->base_1hb)
{
pcr_diff = pcr1->base_32lb - pcr2->base_32lb;
}
else
{
pcr_diff = pcr1->base_32lb - (0xFFFFFFFF - pcr2->base_32lb);
}
/*
** TODO: take extension part of PCR into account
*/
return pcr_diff;
}
static uint32_t get_bitrate(const packet_time_t *time1,
const packet_time_t *time2,
uint32_t *duration)
{
unsigned long long bitrate = 0;
uint32_t pcr_diff;
pcr_diff = pcr1_minus_pcr2(&time1->pcr, &time2->pcr);
if (0 != pcr_diff)
{
bitrate = time1->count - time2->count + 1;
bitrate *= 8 * TS_PACKET_SIZE;
bitrate *= TS_PCR_BASE_CLOCK;
bitrate /= pcr_diff;
}
*duration = pcr_diff / TS_PCR_BASE_CLOCK;
return (uint32_t)bitrate;
}
static void show_bitrate(uint16_t pid, const packet_time_t *time)
{
static uint32_t nb_read_pcrs = 0;
static packet_time_t first_time;
static packet_time_t previous_time;
uint32_t instant_bitrate;
uint32_t average_bitrate;