3
* Copyright (C) 2010-2011 Unix Solutions Ltd.
5
* Released under MIT license.
6
* See LICENSE-MIT.txt for license terms.
15
#define FREE(x) if(x) { free(x); x=NULL; }
18
#define TS_PACKET_SIZE 188
19
#define TS_MAX_PAYLOAD_SIZE (TS_PACKET_SIZE-4)
22
uint8_t sync_byte; // Always 0x47
24
uint16_t tei : 1, // Transport Error Indicator (TEI)
25
pusi : 1, // Payload Unit Start Indicator
26
prio : 1, // Transport Priority
29
uint8_t scramble : 2, // 00 - not scrambled, 01 - reserved, 10 - scrambled with even key, 11 - scrambled with odd key
34
// The variables bellow this line depends may not exist in a packet
35
uint8_t adapt_len; // adaptation field length
36
uint8_t adapt_flags; // adaptation field flags
38
uint8_t payload_size; // Payload size inside the packet
39
uint8_t payload_offset; // Payload offset inside the packet
42
struct ts_section_header {
43
uint8_t pointer_field;
47
uint16_t section_syntax_indicator: 1, // Section Syntax Indicator
48
private_indicator : 1, // Private section indicator
49
reserved1 : 2, // 2 reserved bits
50
section_length : 12; // Section lenth
52
uint16_t ts_id_number; // Transport stream id (in PAT), Program number (in PMT)
54
uint8_t reserved2 : 2,
56
current_next_indicator : 1;
58
uint8_t section_number;
59
uint8_t last_section_number;
61
// The variables bellow this line are not in the physical packet
62
int section_pos; // Up to this pos the section data has come
63
int initialized; // Set to 1 when whole sectino is initialized
65
int section_data_len; // Full section length (3 + section_length)
66
uint8_t *section_data; // The whole section data
67
uint8_t *packet_data; // TS packet(s) that were used to transfer the table.
69
int num_packets; // From how much packets this section is build
71
int data_len; // Data size without the CRC32 (4 bytes)
72
uint8_t *data; // Offset into section_data (where the section data start without the section header)
77
struct ts_pat_program {
84
struct ts_header ts_header;
85
struct ts_section_header *section_header;
87
struct ts_pat_program **programs;
89
// The variables bellow are nor part of the physical packet
90
int programs_max; // How much programs are allocated
91
int programs_num; // How much programs are initialized
92
uint8_t initialized; // Set to 1 when full table is initialized
96
CA_SECA, // 0x0100 - 0x01FF Canal Plus (SECA/Mediaguard)
97
CA_VIACCESS, // 0x0500 - 0x05FF France Telecom
98
CA_IRDETO, // 0x0600 - 0x06FF Irdeto
99
CA_VIDEOGUARD, // 0x0900 - 0x09FF News Datacom (NDS/Videoguard)
100
CA_CONAX, // 0x0B00 - 0x0BFF Norwegian Telekom
101
CA_CRYPTOWORKS, // 0x0D00 - 0x0DFF CrytoWorks
102
CA_NAGRA, // 0x1800 - 0x18FF Kudelski SA (Nagravision)
103
CA_DRECRYPT, // 0x4AE0 - 0x4AE1 OOO Cifra (DRE-Crypt)
108
struct ts_header ts_header;
109
struct ts_section_header *section_header;
111
int program_info_size;
112
uint8_t *program_info;
114
// The variables bellow are nor part of the physical packet
115
uint8_t initialized; // Set to 1 when full table is initialized
118
struct ts_pmt_stream {
121
uint16_t reserved1 : 3,
124
uint16_t reserved2 : 4,
131
struct ts_header ts_header;
132
struct ts_section_header *section_header;
134
uint16_t reserved1 : 3,
137
uint16_t reserved2 : 4,
138
program_info_size : 12;
139
uint8_t *program_info;
141
struct ts_pmt_stream **streams;
143
// The variables bellow are nor part of the physical packet
144
int streams_max; // How much streams are allocated
145
int streams_num; // How much streams are initialized
146
uint8_t initialized; // Set to 1 when full table is initialized
150
struct ts_sdt_stream {
153
uint8_t reserved1 : 6,
154
EIT_schedule_flag : 1,
155
EIT_present_following_flag : 1;
157
uint16_t running_status : 3,
159
descriptor_size : 12;
161
uint8_t *descriptor_data;
165
struct ts_header ts_header;
166
struct ts_section_header *section_header;
168
uint16_t original_network_id;
171
struct ts_sdt_stream **streams;
173
// The variables bellow are nor part of the physical packet
174
int streams_max; // How much streams are allocated
175
int streams_num; // How much streams are initialized
176
uint8_t initialized; // Set to 1 when full table is initialized
181
struct ts_nit_stream {
182
uint16_t transport_stream_id;
183
uint16_t original_network_id;
185
uint16_t reserved1 : 4,
186
descriptor_size : 12;
188
uint8_t *descriptor_data;
192
struct ts_header ts_header;
193
struct ts_section_header *section_header;
195
uint16_t reserved1 : 4,
196
network_info_size : 12;
198
uint8_t *network_info;
200
uint16_t reserved2 : 4,
203
struct ts_nit_stream **streams;
205
// The variables bellow are nor part of the physical packet
206
int streams_max; // How much streams are allocated
207
int streams_num; // How much streams are initialized
208
uint8_t initialized; // Set to 1 when full NIT table is initialized
212
struct ts_eit_stream {
214
uint64_t start_time_mjd : 16,
215
start_time_bcd : 24, // Total 40, start_time
218
uint16_t running_status : 3,
220
descriptor_size : 12;
222
uint8_t *descriptor_data;
226
struct ts_header ts_header;
227
struct ts_section_header *section_header;
229
uint16_t transport_stream_id;
230
uint16_t original_network_id;
231
uint8_t segment_last_section_number;
232
uint8_t last_table_id;
234
struct ts_eit_stream **streams;
236
// The variables bellow are nor part of the physical packet
237
int streams_max; // How much streams are allocated
238
int streams_num; // How much streams are initialized
239
uint8_t initialized; // Set to 1 when full eit table is initialized
243
struct ts_header ts_header;
244
struct ts_section_header *section_header;
246
uint16_t mjd; // This both are part of one 40 bit field (UTC_time)
247
uint32_t bcd; // Only 24 bits are used
249
// The below fields are only in TOT packets, table_id 0x73
250
uint16_t reserved_3 : 4,
251
descriptors_size : 12;
252
uint8_t *descriptors;
254
// The variables bellow are nor part of the physical packet
255
time_t utc; // decoded UTC_time
256
struct tm tm; // decoded UTC_time
262
struct ts_header ts_header;
263
struct ts_section_header *section_header;
268
enum ts_stream_type {
269
STREAM_TYPE_MPEG1_VIDEO = 0x01, // MPEG-1 video
270
STREAM_TYPE_MPEG2_VIDEO = 0x02, // H.262 - MPEG-2 video
272
STREAM_TYPE_MPEG1_AUDIO = 0x03, // MPEG-1 audio
273
STREAM_TYPE_MPEG2_AUDIO = 0x04, // MPEG-2 audio
275
STREAM_TYPE_ADTS_AUDIO = 0x0F, // AAC ADTS
276
STREAM_TYPE_MPEG4_PART2_VIDEO = 0x10, // DIVX - MPEG-4 part 2
278
STREAM_TYPE_AVC_VIDEO = 0x1B, // H.264 - MPEG-4 part 10
279
STREAM_TYPE_AVS_VIDEO = 0x42, // Chinese AVS
281
STREAM_TYPE_DOLBY_DVB_AUDIO = 0x06, // 0x06 - Private stream, look at stream descriptors for AC-3 descriptor
282
STREAM_TYPE_DOLBY_ATSC_AUDIO = 0x81, // 0x81 - Private stream in ATSC (US system, probably we shouldn't care)
285
// ------------------------------------------------------------
286
// PES packet stream ids
287
// See H.222.0 Table 2-17 and Table 2-18
288
#define STREAM_ID_PROGRAM_STREAM_MAP 0xbc
289
#define STREAM_ID_PRIVATE_STREAM_1 0xbd
290
#define STREAM_ID_PADDING_STREAM 0xbe
291
#define STREAM_ID_PRIVATE_STREAM_2 0xbf
292
#define STREAM_ID_ECM_STREAM 0xf0
293
#define STREAM_ID_EMM_STREAM 0xf1
294
#define STREAM_ID_DSMCC_STREAM 0xf2
295
#define STREAM_ID_13522_STREAM 0xf3
296
#define STREAM_ID_H222_A_STREAM 0xf4
297
#define STREAM_ID_H222_B_STREAM 0xf5
298
#define STREAM_ID_H222_C_STREAM 0xf6
299
#define STREAM_ID_H222_D_STREAM 0xf7
300
#define STREAM_ID_H222_E_STREAM 0xf8
301
#define STREAM_ID_ANCILLARY_STREAM 0xf9
302
#define STREAM_ID_PROGRAM_STREAM_DIRECTORY 0xff
304
#define IS_AUDIO_STREAM_ID(id) ((id) >= 0xc0 && (id) <= 0xdf)
305
#define IS_VIDEO_STREAM_ID(id) ((id) >= 0xe0 && (id) <= 0xef)
306
#define IS_PES_STREAM_SUPPORTED(id) (!(id == STREAM_ID_PROGRAM_STREAM_MAP || \
307
id == STREAM_ID_PADDING_STREAM || \
308
id == STREAM_ID_PRIVATE_STREAM_2 || \
309
id == STREAM_ID_ECM_STREAM || \
310
id == STREAM_ID_EMM_STREAM || \
311
id == STREAM_ID_PROGRAM_STREAM_DIRECTORY || \
312
id == STREAM_ID_DSMCC_STREAM || \
313
id == STREAM_ID_H222_E_STREAM))
316
struct mpeg_audio_header {
317
uint32_t syncword : 12,
335
struct ts_header ts_header;
337
uint32_t have_pts : 1, // Have PTS in the PES (init from PES header)
338
have_dts : 1, // Have DTS in the PES (init from PES header)
339
is_audio : 1, // PES carries audio (mpeg2 or AC3) (init from PES stream_id and PMT stream_type and descriptors)
340
is_audio_mpeg1 : 1, // PES carries MP1 audio (init from PMT stream_id)
341
is_audio_mpeg1l1: 1, // PES carries MP1 audio Layer I (init from PMT audio descriptor)
342
is_audio_mpeg1l2: 1, // PES carries MP1 audio Layer II (init from PMT audio descriptor)
343
is_audio_mpeg1l3: 1, // PES carries MP1 audio Layer III (init from PMT audio descriptor)
344
is_audio_mpeg2 : 1, // PES carries MP2 audio (init from PMT stream_id)
345
is_audio_aac : 1, // PES carries AAC audio (init from PMT stream_id)
346
is_audio_ac3 : 1, // PES carries AC3 audio (init from stream_id and PMT descriptors and elmentary stream)
347
is_audio_dts : 1, // PES carries DTS audio (init from stream_id and elementary stream)
348
is_video : 1, // PES carries video (mpeg2 or H.264) (init from PES stream_id)
349
is_video_mpeg1 : 1, // PES carries mpeg1 video (init from PES stream_id)
350
is_video_mpeg2 : 1, // PES carries mpeg2 video (init from PES stream_id)
351
is_video_mpeg4 : 1, // PES carries mpeg4 part 2 video (init from PES stream_id)
352
is_video_h264 : 1, // PES carries H.264 video (init from PES stream_id)
353
is_video_avs : 1, // PES carries AVS video (init from PES stream_id)
354
is_teletext : 1, // PES carries teletext (init from PMT descriptors)
355
is_subtitle : 1; // PES carries subtitles (init from PMT descriptors)
357
uint8_t stream_id; // If !0 then the PES has started initializing
358
uint16_t pes_packet_len; // Allowed to be 0 for video streams
359
int real_pes_packet_len; // if pes_packet_len is > 0 the this is eq to pes_packet_len
360
// if pes_packet_len is = 0 this is set to -1 until very last packet
362
uint8_t flags_1; // Bellow flags
363
uint8_t reserved1 : 2, // Always eq 2 (10 binary)
368
original_or_copy : 1;
370
uint8_t flags_2; // Bellow flags
371
uint8_t PTS_flag : 1,
376
add_copy_info_flag : 1,
378
pes_extension_flag : 1;
380
uint8_t pes_header_len;
382
uint64_t PTS; // if (PTS_flag)
383
uint64_t DTS; // if (DTS_flag)
384
uint64_t ESCR; // if (ESCR_flag)
385
uint32_t ES_rate; // if (ES_rate_flag)
387
uint16_t trick_mode_control : 2, // if (trick_mode_flag)
389
intra_slice_refresh : 1,
394
uint8_t reserved_add : 1, // if (add_copy_info_flag)
397
uint16_t prev_pes_crc; // if (pes_crc_flag)
400
uint8_t flags_3; // Bellow flags
401
uint8_t pes_private_data_flag : 1,
402
pack_header_field_flag : 1,
403
program_packet_seq_counter_flag : 1,
404
p_std_buffer_flag : 1,
406
pes_extension2_flag : 1;
408
uint64_t pes_private_data_1; // if (pes_private_data_flag)
409
uint64_t pes_private_data_2; // The whole field is 128 bits
411
uint8_t pack_header_len; // if (pack_header_field_flag)
412
uint8_t *pack_header; // Pointer into *pes_data
414
uint8_t reserved3 : 1, // if (program_packet_seq_counter_flag)
415
program_packet_seq_counter : 7;
417
uint8_t mpeg1_mpeg2_identifier : 1,
418
original_stuff_length : 6;
420
uint16_t p_std_reserved : 2, // Always 1, if (p_std_buffer_flag)
421
p_std_buffer_scale : 1,
422
p_std_buffer_size : 13;
424
uint16_t reserved4 : 1, // if (pes_extension2_flag)
425
pes_extension_field_len : 7;
426
uint8_t *pes_extension2; // Pointer into *pes_data
429
uint8_t *pes_data; // Whole packet is stored here
430
uint32_t pes_data_pos; // How much data is filled in pes_data
431
uint32_t pes_data_size; // Total allocated for pes_data
432
uint8_t pes_data_initialized; // Set to 1 when all of the pes_data is in *pes_data and the parsing can start
435
uint8_t *es_data; // Pointer to start of data after PES header, initialized when the packet is fully assembled
436
uint32_t es_data_size; // Full pes packet length (used for video streams, otherwise equal to pes_packet_len)
437
uint8_t initialized; // Set to 1 when the packet is fully assembled
440
struct mpeg_audio_header mpeg_audio_header;
446
struct ts_pes *pes_next;
452
struct pes_entry **entries;
455
typedef uint8_t pidmap_t[0x2000];