summaryrefslogtreecommitdiff
path: root/mcast/common/.svn/text-base/siparser.h.svn-base
blob: 255ebe05a6cdc5ef777ebd5a0221f189d78976ed (plain)
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
#ifndef __SIPARSER_H__
#define __SIPARSER_H__

#define TS_SECT_LEN(buf) \
	unsigned char *ptr = buf; \
	int slen = (((ptr[1] << 8) | ptr[2]) & 0x0fff); 
	

#define TS_PACKET_LEN (188)              /* TS RDSIZE is fixed !! */
#define TS_SYNC_BYTE  (0x47)             /* SyncByte for TS  ISO 138181-1 */
#define TS_BUF_SIZE   (256 * 1024)       /* default DMX_Buffer Size for TS */
#define PSI_BUF_SIZE  (2 * 4096)	/* Section length max. 12 bits */
#define READ_BUF_SIZE (256*TS_PACKET_LEN)  /* min. 2x TS_PACKET_LEN!!! */
#define BILLION  1000000000L;
#define MAX_DESC_LEN 255 //descriptor_length field 8-bit ISO/IEC 13818-1
#define MAX_ES_PIDS 32


#define VIDEO_11172_STREAM_TYPE 			0x1 // STREAMTYPE_11172_VIDEO
#define VIDEO_13818_STREAM_TYPE				0x2 // STREAMTYPE_13818_VIDEO
#define VISUAL_MPEG4_STREAM_TYPE			0x10 // 14496-2 Visual MPEG-4
#define VIDEO_H264_STREAM_TYPE              		0x1b // 14496-10 Video h.264
#define AUDIO_11172_STREAM_TYPE             		0x3 // STREAMTYPE_11172_AUDIO
#define AUDIO_13818_STREAM_TYPE 			0x4 // STREAMTYPE_13818_AUDIO
#define PRIVATE_13818_STREAM_TYPE	               	0x5 // STREAMTYPE_13818_PRIVATE
#define PRIVATE_13818_PES_STREAM_TYPE			0x6 // STREAMTYPE_13818_PES_PRIVATE 

enum DescriptorTag {
  // defined by ISO/IEC 13818-1
               VideoStreamDescriptorTag = 0x02,
               AudioStreamDescriptorTag = 0x03,
               HierarchyDescriptorTag = 0x04,
               RegistrationDescriptorTag = 0x05,
               DataStreamAlignmentDescriptorTag = 0x06,
               TargetBackgroundGridDescriptorTag = 0x07,
               VideoWindowDescriptorTag = 0x08,
               CaDescriptorTag = 0x09,
               ISO639LanguageDescriptorTag = 0x0A,
               SystemClockDescriptorTag = 0x0B,
               MultiplexBufferUtilizationDescriptorTag = 0x0C,
               CopyrightDescriptorTag = 0x0D,
               MaximumBitrateDescriptorTag = 0x0E,
               PrivateDataIndicatorDescriptorTag = 0x0F,
               SmoothingBufferDescriptorTag = 0x10,
               STDDescriptorTag = 0x11,
               IBPDescriptorTag = 0x12,
  // defined by ISO-13818-6 (DSM-CC)
               CarouselIdentifierDescriptorTag = 0x13,
               // 0x14 - 0x3F  Reserved
  // defined by ETSI (EN 300 468)
               NetworkNameDescriptorTag = 0x40,
               ServiceListDescriptorTag = 0x41,
               StuffingDescriptorTag = 0x42,
               SatelliteDeliverySystemDescriptorTag = 0x43,
               CableDeliverySystemDescriptorTag = 0x44,
               VBIDataDescriptorTag = 0x45,
               VBITeletextDescriptorTag = 0x46,
               BouquetNameDescriptorTag = 0x47,
               ServiceDescriptorTag = 0x48,
               CountryAvailabilityDescriptorTag = 0x49,
               LinkageDescriptorTag = 0x4A,
               NVODReferenceDescriptorTag = 0x4B,
               TimeShiftedServiceDescriptorTag = 0x4C,
               ShortEventDescriptorTag = 0x4D,
               ExtendedEventDescriptorTag = 0x4E,
               TimeShiftedEventDescriptorTag = 0x4F,
               ComponentDescriptorTag = 0x50,
               MocaicDescriptorTag = 0x51,
               StreamIdentifierDescriptorTag = 0x52,
               CaIdentifierDescriptorTag = 0x53,
               ContentDescriptorTag = 0x54,
               ParentalRatingDescriptorTag = 0x55,
               TeletextDescriptorTag = 0x56,
               TelephoneDescriptorTag = 0x57,
               LocalTimeOffsetDescriptorTag = 0x58,
               SubtitlingDescriptorTag = 0x59,
               TerrestrialDeliverySystemDescriptorTag = 0x5A,
               MultilingualNetworkNameDescriptorTag = 0x5B,
               MultilingualBouquetNameDescriptorTag = 0x5C,
               MultilingualServiceNameDescriptorTag = 0x5D,
               MultilingualComponentDescriptorTag = 0x5E,
               PrivateDataSpecifierDescriptorTag = 0x5F,
               ServiceMoveDescriptorTag = 0x60,
               ShortSmoothingBufferDescriptorTag = 0x61,
               FrequencyListDescriptorTag = 0x62,
               PartialTransportStreamDescriptorTag = 0x63,
               DataBroadcastDescriptorTag = 0x64,
               ScramblingDescriptorTag = 0x65,
               DataBroadcastIdDescriptorTag = 0x66,
               TransportStreamDescriptorTag = 0x67,
               DSNGDescriptorTag = 0x68,
               PDCDescriptorTag = 0x69,
               AC3DescriptorTag = 0x6A,
               AncillaryDataDescriptorTag = 0x6B,
               CellListDescriptorTag = 0x6C,
               CellFrequencyLinkDescriptorTag = 0x6D,
               AnnouncementSupportDescriptorTag = 0x6E,
               ApplicationSignallingDescriptorTag = 0x6F,
               AdaptationFieldDataDescriptorTag = 0x70,
               ServiceIdentifierDescriptorTag = 0x71,
               ServiceAvailabilityDescriptorTag = 0x72,
  // defined by ETSI (EN 300 468) v 1.7.1
               DefaultAuthorityDescriptorTag = 0x73,
               RelatedContentDescriptorTag = 0x74,
               TVAIdDescriptorTag = 0x75,
               ContentIdentifierDescriptorTag = 0x76,
               TimeSliceFecIdentifierDescriptorTag = 0x77,
               ECMRepetitionRateDescriptorTag = 0x78,
               S2SatelliteDeliverySystemDescriptorTag = 0x79,
               EnhancedAC3DescriptorTag = 0x7A,
               DTSDescriptorTag = 0x7B,
               AACDescriptorTag = 0x7C,
               ExtensionDescriptorTag = 0x7F,

 // Defined by ETSI TS 102 812 (MHP)
               // They once again start with 0x00 (see page 234, MHP specification)
               MHP_ApplicationDescriptorTag = 0x00,
               MHP_ApplicationNameDescriptorTag = 0x01,
               MHP_TransportProtocolDescriptorTag = 0x02,
               MHP_DVBJApplicationDescriptorTag = 0x03,
               MHP_DVBJApplicationLocationDescriptorTag = 0x04,
               // 0x05 - 0x0A is unimplemented this library
               MHP_ExternalApplicationAuthorisationDescriptorTag = 0x05,
               MHP_IPv4RoutingDescriptorTag = 0x06,
               MHP_IPv6RoutingDescriptorTag = 0x07,
               MHP_DVBHTMLApplicationDescriptorTag = 0x08,
               MHP_DVBHTMLApplicationLocationDescriptorTag = 0x09,
               MHP_DVBHTMLApplicationBoundaryDescriptorTag = 0x0A,
               MHP_ApplicationIconsDescriptorTag = 0x0B,
               MHP_PrefetchDescriptorTag = 0x0C,
               MHP_DelegatedApplicationDescriptorTag = 0x0E,
               MHP_ApplicationStorageDescriptorTag = 0x10,
  // Premiere private Descriptor Tags
               PremiereContentTransmissionDescriptorTag = 0xF2,

               //a descriptor currently unimplemented in this library
               //the actual value 0xFF is "forbidden" according to the spec.
               UnimplementedDescriptorTag = 0xFF
};



typedef struct ts_packet_hdr 
{
  unsigned int sync_byte;
  unsigned int transport_error_indicator;
  unsigned int payload_unit_start_indicator;
  unsigned int transport_priority;
  unsigned int pid;
  unsigned int transport_scrambling_control;
  unsigned int adaptation_field_control;
  unsigned int continuity_counter;
} ts_packet_hdr_t;

typedef struct  pat {
  unsigned int table_id;
  unsigned int section_syntax_indicator;		
  unsigned int reserved_1;
  unsigned int section_length;
  unsigned int transport_stream_id;
  unsigned int reserved_2;
  unsigned int version_number;
  unsigned int current_next_indicator;
  unsigned int section_number;
  unsigned int last_section_number;

  // FIXME: list of programs

  unsigned int crc32;
} pat_t;

typedef struct _pat_list {
  unsigned int program_number; //SID
  unsigned int reserved;
  unsigned int network_pmt_pid;

  int cads_present;
  int cads_num;

} pat_list_t;

typedef struct pmt_pid_list {

  pat_t p;
  pat_list_t *pl;
  unsigned int pmt_pids;
 
} pmt_pid_list_t;

typedef struct psi_buf {

  unsigned char *buf;
  unsigned int len;//used for offset
  unsigned int start;  

  int pid;
  int continuity;

} psi_buf_t;

typedef struct pmt {
    unsigned int table_id;
    unsigned int section_syntax_indicator;		
    unsigned int reserved_1;
    unsigned int section_length;
    unsigned int program_number;
    unsigned int reserved_2;
    unsigned int version_number;
    unsigned int current_next_indicator;
    unsigned int section_number;
    unsigned int last_section_number;
    unsigned int reserved_3;
    unsigned int pcr_pid;
    unsigned int reserved_4;
    unsigned int program_info_length;

    // N descriptors
    
    // N1 stream types and descriptors

    unsigned int crc32;  
} pmt_t;

typedef struct es_pmt_info {
    unsigned int stream_type;
    unsigned int reserved_1; 
    unsigned int elementary_pid;
    unsigned int reserved_2;
    unsigned int es_info_length;

    // N2 descriptor

} es_pmt_info_t;

typedef struct  ca_descriptor {
    
    unsigned int descriptor_tag;
    unsigned int descriptor_length;		
    unsigned int  ca_system_id;
    unsigned int  reserved;
    unsigned int  ca_pid;
    unsigned char private_data[MAX_DESC_LEN];

} si_desc_t;

typedef struct pmt_descriptor {

    pmt_t pmt_hdr;

    int cas;
    si_desc_t *cad;

} si_pmt_desc_t;

typedef struct ca_descriptor_list {

    int cads;
    si_desc_t *cad;

} si_cad_t;

typedef struct ca_sid_info {

    int sid;
    int version;
    int offset;
    int len;
  
} ca_sid_t;

typedef struct ca_pmt_descriptors {

    int cads;
    int size;
    unsigned char *cad;
            
} si_ca_pmt_t;

typedef struct ca_es_pid_info {

    int pid;
    uint8_t type;

} ca_es_pid_info_t;

typedef struct ca_pmt_list {
    
    int sid;
    int pmt_pid;
    
    pmt_t p;
    si_ca_pmt_t pm;
    si_ca_pmt_t es;    

    ca_es_pid_info_t espids[MAX_ES_PIDS];    
    int es_pid_num;
  
} ca_pmt_list_t;


typedef struct ca_sid_list {

    int tc; //total number of CA desc.
    int num;
    ca_pmt_list_t *l;

} ca_sid_list_t;

typedef struct  _cat {
    unsigned int table_id;
    unsigned int section_syntax_indicator;		
    unsigned int reserved_1;
    unsigned int section_length;
    unsigned int reserved_2;
    unsigned int version_number;
    unsigned int current_next_indicator;
    unsigned int section_number;
    unsigned int last_section_number;

    // private section
    
    unsigned int crc32;
} cat_t;

typedef struct tdt_sect {

      uint8_t  table_id;
      uint8_t  section_syntax_indicator;
      uint8_t  reserved; //0 future use
      uint8_t  reserved_1;
      uint16_t section_length;
      uint8_t  dvbdate[5];
} tdt_sect_t;

typedef struct _str_table {
    unsigned int       from;          
    unsigned int       to;            
    const char  	*str;          
} str_table;
            

int parse_ca_descriptor(unsigned char *desc, si_desc_t *t);

int ts2psi_data(unsigned char *buf,psi_buf_t *p,int len, int pid_req);	
int parse_pat_sect(unsigned char *buf, int size,  pmt_pid_list_t *pmt);
int parse_pmt_ca_desc(unsigned char *buf, int size, int sid, si_ca_pmt_t *pm_cads, si_ca_pmt_t *es_cads, pmt_t *pmt_hdr, int *fta, ca_es_pid_info_t *espid, int *es_pid_num);
int parse_cat_sect(unsigned char *buf, int size, si_cad_t *emm);
int parse_tdt_sect(unsigned char *buf, int size, tdt_sect_t *tdt);
int get_ts_packet_hdr(unsigned char *buf, ts_packet_hdr_t *p);
int si_get_video_pid(unsigned char *esi_buf, int size, int *vpid);
int si_get_audio_pid(unsigned char *esi_buf, int size, int *apid);
int si_get_private_pids(unsigned char *esi_buf, int size, int *upids);
int get_pmt_es_pids(unsigned char *esi_buf, int size, int *es_pids, int all);
void print_pat(pat_t *p, pat_list_t *pl, int pmt_num);
void printhex_buf(char *msg,unsigned char *buf,int len);
void writehex_buf(FILE *f,char *msg,unsigned char *buf,int len);
void print_cad_lst(si_cad_t *l, int ts_id);
void print_ca_bytes(si_desc_t *p);
void get_time_mjd (unsigned long mjd, long *year , long *month, long *day);
void print_tdt(tdt_sect_t *tdt, uint16_t mjd, uint32_t utc);
int ca_free_cpl_desc(ca_pmt_list_t *cpl);
char *si_caid_to_name(unsigned int caid);

#endif