~ubuntu-branches/debian/jessie/tsdecrypt/jessie

« back to all changes in this revision

Viewing changes to libtsfuncs/tsdata.h

  • Committer: Package Import Robot
  • Author(s): Alessio Treglia
  • Date: 2012-04-04 09:42:43 UTC
  • Revision ID: package-import@ubuntu.com-20120404094243-qsc40h18oolnxw5r
Tags: upstream-7.0
ImportĀ upstreamĀ versionĀ 7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Data defintions
 
3
 * Copyright (C) 2010-2011 Unix Solutions Ltd.
 
4
 *
 
5
 * Released under MIT license.
 
6
 * See LICENSE-MIT.txt for license terms.
 
7
 */
 
8
#ifndef LIBTS_TSDATA_H
 
9
#define LIBTS_TSDATA_H
 
10
 
 
11
#include <netdb.h>
 
12
#include <time.h>
 
13
 
 
14
#ifndef FREE
 
15
        #define FREE(x) if(x) { free(x); x=NULL; }
 
16
#endif
 
17
 
 
18
#define TS_PACKET_SIZE       188
 
19
#define TS_MAX_PAYLOAD_SIZE  (TS_PACKET_SIZE-4)
 
20
 
 
21
struct ts_header {
 
22
        uint8_t         sync_byte;                      // Always 0x47
 
23
 
 
24
        uint16_t        tei           : 1,      // Transport Error Indicator (TEI)
 
25
                                pusi          : 1,      // Payload Unit Start Indicator
 
26
                                prio          : 1,      // Transport Priority
 
27
                                pid           : 13;     // PID
 
28
 
 
29
        uint8_t         scramble      : 2,      // 00 - not scrambled, 01 - reserved, 10 - scrambled with even key,  11 - scrambled with odd key
 
30
                                adapt_field   : 1,
 
31
                                payload_field : 1,
 
32
                                continuity    : 4;
 
33
 
 
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
 
37
 
 
38
        uint8_t         payload_size;           // Payload size inside the packet
 
39
        uint8_t         payload_offset;         // Payload offset inside the packet
 
40
};
 
41
 
 
42
struct ts_section_header {
 
43
        uint8_t         pointer_field;
 
44
 
 
45
        uint8_t         table_id;
 
46
 
 
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
 
51
 
 
52
        uint16_t        ts_id_number;                                   // Transport stream id (in PAT), Program number (in PMT)
 
53
 
 
54
        uint8_t         reserved2              : 2,
 
55
                                version_number         : 5,
 
56
                                current_next_indicator : 1;
 
57
 
 
58
        uint8_t         section_number;
 
59
        uint8_t         last_section_number;
 
60
 
 
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
 
64
 
 
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.
 
68
 
 
69
        int                     num_packets;                                    // From how much packets this section is build
 
70
 
 
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)
 
73
 
 
74
        uint32_t        CRC;
 
75
};
 
76
 
 
77
struct ts_pat_program {
 
78
        uint16_t        program;
 
79
        uint16_t        reserved:3,
 
80
                                pid:13;
 
81
};
 
82
 
 
83
struct ts_pat {
 
84
        struct ts_header                        ts_header;
 
85
        struct ts_section_header        *section_header;
 
86
 
 
87
        struct ts_pat_program           **programs;
 
88
 
 
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
 
93
};
 
94
 
 
95
enum CA_system {
 
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)
 
104
        CA_UNKNOWN,
 
105
};
 
106
 
 
107
struct ts_cat {
 
108
        struct ts_header                        ts_header;
 
109
        struct ts_section_header        *section_header;
 
110
 
 
111
        int                                                     program_info_size;
 
112
        uint8_t                                         *program_info;
 
113
 
 
114
        // The variables bellow are nor part of the physical packet
 
115
        uint8_t                                         initialized;    // Set to 1 when full table is initialized
 
116
};
 
117
 
 
118
struct ts_pmt_stream {
 
119
        uint8_t         stream_type;
 
120
 
 
121
        uint16_t        reserved1    : 3,
 
122
                                pid          : 13;
 
123
 
 
124
        uint16_t        reserved2    : 4,
 
125
                                ES_info_size : 12;
 
126
 
 
127
        uint8_t         *ES_info;
 
128
};
 
129
 
 
130
struct ts_pmt {
 
131
        struct ts_header                        ts_header;
 
132
        struct ts_section_header        *section_header;
 
133
 
 
134
        uint16_t                                        reserved1         : 3,
 
135
                                                                PCR_pid           : 13;
 
136
 
 
137
        uint16_t                                        reserved2         : 4,
 
138
                                                                program_info_size : 12;
 
139
        uint8_t                                         *program_info;
 
140
 
 
141
        struct ts_pmt_stream            **streams;
 
142
 
 
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
 
147
};
 
148
 
 
149
 
 
150
struct ts_sdt_stream {
 
151
        uint16_t        service_id;
 
152
 
 
153
        uint8_t         reserved1                  : 6,
 
154
                                EIT_schedule_flag          : 1,
 
155
                                EIT_present_following_flag : 1;
 
156
 
 
157
        uint16_t        running_status             : 3,
 
158
                                free_CA_mode               : 1,
 
159
                                descriptor_size            : 12;
 
160
 
 
161
        uint8_t         *descriptor_data;
 
162
};
 
163
 
 
164
struct ts_sdt {
 
165
        struct ts_header                        ts_header;
 
166
        struct ts_section_header        *section_header;
 
167
 
 
168
        uint16_t                                        original_network_id;
 
169
        uint8_t                                         reserved;
 
170
 
 
171
        struct ts_sdt_stream            **streams;
 
172
 
 
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
 
177
};
 
178
 
 
179
 
 
180
 
 
181
struct ts_nit_stream {
 
182
        uint16_t        transport_stream_id;
 
183
        uint16_t        original_network_id;
 
184
 
 
185
        uint16_t        reserved1       : 4,
 
186
                                descriptor_size : 12;
 
187
 
 
188
        uint8_t         *descriptor_data;
 
189
};
 
190
 
 
191
struct ts_nit {
 
192
        struct ts_header                        ts_header;
 
193
        struct ts_section_header        *section_header;
 
194
 
 
195
        uint16_t                                        reserved1         : 4,
 
196
                                                                network_info_size : 12;
 
197
 
 
198
        uint8_t                                         *network_info;
 
199
 
 
200
        uint16_t                                        reserved2         : 4,
 
201
                                                                ts_loop_size      : 12;
 
202
 
 
203
        struct ts_nit_stream            **streams;
 
204
 
 
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
 
209
};
 
210
 
 
211
 
 
212
struct ts_eit_stream {
 
213
        uint16_t        event_id;
 
214
        uint64_t        start_time_mjd  : 16,
 
215
                                start_time_bcd  : 24,   // Total 40, start_time
 
216
                                duration_bcd    : 24;
 
217
 
 
218
        uint16_t        running_status  : 3,
 
219
                                free_CA_mode    : 1,
 
220
                                descriptor_size : 12;
 
221
 
 
222
        uint8_t         *descriptor_data;
 
223
};
 
224
 
 
225
struct ts_eit {
 
226
        struct ts_header                        ts_header;
 
227
        struct ts_section_header        *section_header;
 
228
 
 
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;
 
233
 
 
234
        struct ts_eit_stream            **streams;
 
235
 
 
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
 
240
};
 
241
 
 
242
struct ts_tdt {
 
243
        struct ts_header                        ts_header;
 
244
        struct ts_section_header        *section_header;
 
245
 
 
246
        uint16_t        mjd;                                                    // This both are part of one 40 bit field (UTC_time)
 
247
        uint32_t        bcd;                                                    // Only 24 bits are used
 
248
 
 
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;
 
253
 
 
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
 
257
 
 
258
        uint8_t         initialized;
 
259
};
 
260
 
 
261
struct ts_privsec {
 
262
        struct ts_header                        ts_header;
 
263
        struct ts_section_header        *section_header;
 
264
        uint8_t                                         initialized;
 
265
};
 
266
 
 
267
// PMT stream types
 
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
 
271
 
 
272
        STREAM_TYPE_MPEG1_AUDIO                 = 0x03, // MPEG-1 audio
 
273
        STREAM_TYPE_MPEG2_AUDIO                 = 0x04, // MPEG-2 audio
 
274
 
 
275
        STREAM_TYPE_ADTS_AUDIO                  = 0x0F, // AAC ADTS
 
276
        STREAM_TYPE_MPEG4_PART2_VIDEO   = 0x10, // DIVX - MPEG-4 part 2
 
277
 
 
278
        STREAM_TYPE_AVC_VIDEO                   = 0x1B, // H.264 - MPEG-4 part 10
 
279
        STREAM_TYPE_AVS_VIDEO                   = 0x42, // Chinese AVS
 
280
 
 
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)
 
283
};
 
284
 
 
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
 
303
 
 
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))
 
314
 
 
315
 
 
316
struct mpeg_audio_header {
 
317
        uint32_t        syncword                : 12,
 
318
                                ID                              : 1,
 
319
                                layer                   : 2,
 
320
                                protection_bit  : 1,
 
321
                                bitrate_index   : 4,
 
322
                                sampl_freq              : 2,
 
323
                                padding_bit             : 1,
 
324
                                private_bit             : 1,
 
325
                                mode                    : 2,
 
326
                                mode_extension  : 2,
 
327
                                copyright               : 1,
 
328
                                org_home                : 1,
 
329
                                emphasis                : 2;
 
330
 
 
331
        uint8_t         initialized;
 
332
};
 
333
 
 
334
struct ts_pes {
 
335
        struct ts_header ts_header;
 
336
 
 
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)
 
356
 
 
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
 
361
 
 
362
        uint8_t         flags_1;                                        // Bellow flags
 
363
        uint8_t         reserved1                       : 2,    // Always eq 2 (10 binary)
 
364
                                scrambling                      : 2,
 
365
                                priority                        : 1,
 
366
                                data_alignment          : 1,
 
367
                                copyright                       : 1,
 
368
                                original_or_copy        : 1;
 
369
 
 
370
        uint8_t         flags_2;                                        // Bellow flags
 
371
        uint8_t         PTS_flag                        : 1,
 
372
                                DTS_flag                        : 1,
 
373
                                ESCR_flag                       : 1,
 
374
                                ES_rate_flag            : 1,
 
375
                                trick_mode_flag         : 1,
 
376
                                add_copy_info_flag      : 1,
 
377
                                pes_crc_flag            : 1,
 
378
                                pes_extension_flag      : 1;
 
379
 
 
380
        uint8_t         pes_header_len;
 
381
 
 
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)
 
386
 
 
387
        uint16_t        trick_mode_control      : 2,    // if (trick_mode_flag)
 
388
                                field_id                        : 2,
 
389
                                intra_slice_refresh     : 1,
 
390
                                freq_truncation         : 2,
 
391
                                rep_ctrl                        : 5,
 
392
                                tm_reserved                     : 4;
 
393
 
 
394
        uint8_t         reserved_add            : 1,    // if (add_copy_info_flag)
 
395
                                add_copy_info           : 7;
 
396
 
 
397
        uint16_t        prev_pes_crc;                           // if (pes_crc_flag)
 
398
 
 
399
        // PES extension
 
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,
 
405
                                reserved2                                               : 3,
 
406
                                pes_extension2_flag                             : 1;
 
407
 
 
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
 
410
 
 
411
        uint8_t         pack_header_len;                                        // if (pack_header_field_flag)
 
412
        uint8_t         *pack_header;                                           // Pointer into *pes_data
 
413
 
 
414
        uint8_t         reserved3                                       : 1,    // if (program_packet_seq_counter_flag)
 
415
                                program_packet_seq_counter      : 7;
 
416
 
 
417
        uint8_t         mpeg1_mpeg2_identifier          : 1,
 
418
                                original_stuff_length           : 6;
 
419
 
 
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;
 
423
 
 
424
        uint16_t        reserved4                                       : 1,    // if (pes_extension2_flag)
 
425
                                pes_extension_field_len         : 7;
 
426
        uint8_t         *pes_extension2;                                        // Pointer into *pes_data
 
427
 
 
428
        // Private 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
 
433
 
 
434
        // More private data
 
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
 
438
 
 
439
        // Extra data
 
440
        struct mpeg_audio_header mpeg_audio_header;
 
441
};
 
442
 
 
443
struct pes_entry {
 
444
        uint16_t                pid;
 
445
        struct ts_pes   *pes;
 
446
        struct ts_pes   *pes_next;
 
447
};
 
448
 
 
449
struct pes_array {
 
450
        int max;
 
451
        int cur;
 
452
        struct pes_entry **entries;
 
453
};
 
454
 
 
455
typedef uint8_t pidmap_t[0x2000];
 
456
 
 
457
#endif