1 /*
2  *			GPAC - Multimedia Framework C SDK
3  *
4  *			Authors: Walid B.H - Jean Le Feuvre
5  *			Copyright (c) Telecom ParisTech 2000-2012
6  *					All rights reserved
7  *
8  *  This file is part of GPAC / MPEG2-TS sub-project
9  *
10  *  GPAC is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU Lesser General Public License as published by
12  *  the gf_free Software Foundation; either version 2, or (at your option)
13  *  any later version.
14  *
15  *  GPAC is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU Lesser General Public License for more details.
19  *
20  *  You should have received a copy of the GNU Lesser General Public
21  *  License along with this library; see the file COPYING.  If not, write to
22  *  the gf_free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25 
26 
27 #ifndef _GF_DVB_MPE_DEV_H_
28 #define _GF_DVB_MPE_DEV_H_
29 
30 #include <gpac/dvb_mpe.h>
31 #include <gpac/internal/reedsolomon.h>
32 
33 
34 #ifndef GPAC_DISABLE_MPEG2TS
35 
36 
37 /*INT object*/
38 typedef struct
39 {
40 	u32 id;
41 	u32 processing_order;
42 	u32 number_of_descriptor;
43 	GF_List * descriptors;
44 
45 } GF_M2TS_INT;
46 
47 typedef struct
48 {
49 	u32 tag;
50 	u32 length;
51 	u32 network_id;
52 	u32 original_network_id;
53 	u32 ts_id;
54 	u32 service_id;
55 	u32 component_tag;
56 
57 } GF_M2TS_LOC_DSCPTR_IP_STREAM;
58 
59 typedef struct descriptor_TimeSliceFec
60 {
61 	Bool time_slicing;
62 	u8 mpe_fec;
63 	u8 frame_size;
64 	u8 max_burst_duration;
65 	u8 max_average_rate;
66 	u8 time_slice_fec_id;
67 	u8 * id_selector;
68 } GF_M2TS_DesTimeSliceFec;
69 
70 typedef struct
71 {
72 	u16	network_id;
73 	u16 original_network_id;
74 	u16 transport_stream_id;
75 	u16 service_id;
76 	u8 component_tag;
77 } GF_M2TS_DesLocation;
78 
79 typedef struct {
80 	u8 type; /* 0 = target_IP_descriptor, 1 = target_IP_address_descriptor */
81 	u32 address_mask;
82 	u8 address[4];
83 	u8 slash_mask;
84 	u32 rx_port[10];   /* list of the address port */
85 } GF_M2TS_IP_Target;
86 
87 typedef struct
88 {
89 	GF_List *targets; /* list of IP destination for the IP streams in the platform */
90 	u32 PID;
91 	Bool stream_info_gathered;
92 
93 	/* location descriptor only valid for the associated targets */
94 	GF_M2TS_DesLocation location;
95 
96 	GF_M2TS_DesTimeSliceFec time_slice_fec;
97 } GF_M2TS_IP_Stream;
98 
99 
100 /*IP_Platform object*/
101 typedef struct __gf_dvb_mpe_ip_platform
102 {
103 	/* remaining from INT, to be delete */
104 	u32 id;
105 	u32 processing_order;
106 	u32 number_of_descriptor;
107 
108 	u8 *name; /* platform name */
109 	u8 *provider_name; /* platform provider name */
110 
111 	/* location descriptor valid for the whole platform */
112 	GF_M2TS_DesLocation *location;
113 
114 	GF_List * ip_streams;
115 	Bool all_info_gathered;
116 	GF_List *socket_struct;
117 
118 } GF_M2TS_IP_PLATFORM;
119 
120 typedef struct
121 {
122 	u8 *data;                        /* Data */
123 	u32 u32_version;                   /* IP version */
124 	u32 u32_hdr_length;                /* header length by piece of 4 bytes */
125 	u32 u32_total_length;              /* the length of the datagram (hdr+payload) in bytes */
126 	u32 u32_payload_size;              /* the length of the payload */
127 	u32 u32_id_nb;                     /* the number of the paquet, in case of frag */
128 	u32 u32_flag;                      /* if 010 unfrag packet, 100 fragmented packet, check the id_nb to know the packet number.
129 	                                      0 is the last one */
130 	u32 u32_frag_offset;               /* The offset position of this packet compare to the first packet. unit : 8 bytes */
131 	u32 u32_TTL;                       /* (Time To Live) when = 0 , the packet is ignored and error message */
132 	u32 u32_protocol;                  /* TCP = 6, UDP = 17, ICMP = 1 */
133 	u32 u32_crc;
134 	u8 u8_tx_adr[4];                   /* source address */
135 	u8 u8_rx_adr[4];                   /* destination address */
136 	u32 u32_size_option;               /* size of the option before payload */
137 	u32 u32_padding;                   /* = 1 if where read padding columns */
138 	u32 u32_sum;
139 
140 	/* UDP */
141 	u32 u32_tx_udp_port;                /* source port */
142 	u32 u32_rx_udp_port;                /* destination port */
143 	u32 u32_udp_data_size;
144 	u32 u32_udp_chksm;
145 } GF_M2TS_IP_Packet;
146 
147 
148 
149 
150 #define MPE_ADT_COLS 191
151 #define MPE_RS_COLS NPAR
152 
153 typedef struct mpe_error_holes
154 {
155 	u32 offset;
156 	u32 length;
157 
158 } MPE_Error_Holes;
159 
160 typedef struct mpe_fec_frame
161 {
162 	u32 rows;
163 	u32 col_adt ;
164 	u32 col_rs ;
165 	u8 *p_adt; /* pointer to the application data table*/
166 	u8 *p_rs;  /* pointer to the RS data table*/
167 	u32 *p_error_adt;
168 	u32 *p_error_rs ;
169 
170 	u32 capacity_total;
171 	u32 current_offset_adt ;
172 	u32 current_offset_rs;
173 	u32 initialized ;
174 	u8  ADT_done;
175 	u32 PID;
176 	GF_List *mpe_holes;
177 	//u32 erasures [] p_erasures; /*pointer to the error indicators*/
178 } MPE_FEC_FRAME;
179 
180 
181 /* Get INT table */
182 void gf_m2ts_process_int(GF_M2TS_Demuxer *ts, GF_M2TS_SECTION_ES *ip_table, u8 *data, u32 data_size, u32 table_id);
183 
184 void section_DSMCC_INT(GF_M2TS_IP_PLATFORM* ip_platform, u8 *data, u32 data_size);
185 
186 u32  platform_descriptorDSMCC_INT_UNT  (GF_M2TS_IP_PLATFORM* ip_platform,u8 *data);
187 u32 dsmcc_pto_platform_descriptor_loop (GF_M2TS_IP_PLATFORM* ip_platform,u8 *data);
188 
189 u32  descriptorDSMCC_INT_UNT  (GF_M2TS_IP_Stream *ip_str,u8 *data);
190 void descriptorDSMCC_target_IP_address ( GF_M2TS_IP_Stream *ip_str, u8 *data);
191 u32 dsmcc_pto_descriptor_loop (GF_M2TS_IP_Stream *ip_str, u8 *data);
192 void descriptorTime_slice_fec_identifier(GF_M2TS_IP_Stream *ip_str, u8 *data);
193 void gf_m2ts_target_ip( GF_M2TS_IP_Stream* ip_str, u8 *data);
194 void descriptorLocation(GF_M2TS_IP_Stream *ip_str , u8 *data);
195 
196 
197 void gf_ip_platform_descriptor(GF_M2TS_IP_PLATFORM* ip_platform, u8 *data);
198 void gf_ip_platform_provider_descriptor(GF_M2TS_IP_PLATFORM* ip_platform,u8 *data);
199 void gf_m2ts_ip_platform_init(GF_M2TS_IP_PLATFORM * ip_platform);
200 
201 u32 gf_m2ts_ipdatagram_reader(u8 *datagram, GF_M2TS_IP_Packet *ip_packet, u32 offset);
202 void gf_m2ts_process_ipdatagram(MPE_FEC_FRAME *mff,GF_M2TS_Demuxer *ts);
203 Bool gf_m2ts_compare_ip(u8 rx_ip_address[4], u8 ip_address_bootstrap[4]);
204 
205 struct _sock_entry
206 {
207 	u32 ipv4_addr;
208 	u16 port;
209 	GF_Socket *sock;
210 	Bool bind_failure;
211 };
212 struct tag_m2ts_section_mpe
213 {
214 	ABSTRACT_ES
215 	GF_M2TS_SectionFilter *sec;
216 
217 	/* if this stream is an MPE section stream, we need:
218 	    - a direct access to the timeslice fec descriptor
219 		- an MPE FEC Frame Structure to process RS code */
220 	GF_M2TS_IP_Stream *ip_platform;
221 	MPE_FEC_FRAME *mff;
222 
223 };
224 
225 
226 void gf_m2ts_process_mpe(GF_M2TS_Demuxer *ts, GF_M2TS_SECTION_MPE *mpe, u8 *data, u32 data_size, u8 table_id);
227 void gf_m2ts_gather_ipdatagram_information(MPE_FEC_FRAME *mff,GF_M2TS_Demuxer *ts);
228 
229 void socket_simu(GF_M2TS_IP_Packet *ip_packet, GF_M2TS_Demuxer *ts, Bool yield);
230 
231 void gf_m2ts_mpe_send_datagram(GF_M2TS_Demuxer *ts, u32 pid, u8 *data, u32 data_size);
232 
233 /* allocate the necessary memory space*/
234 u32 init_frame(MPE_FEC_FRAME * mff, u32 rows);
235 
236 void getRowFromADT(MPE_FEC_FRAME * mff,u32 index, u8 * adt_row);
237 void getRowFromRS(MPE_FEC_FRAME * mff,u32 index, u8 * rs_row);
238 void setRowRS(MPE_FEC_FRAME * mff,u32 index, u8 * p_rs);
239 
240 /*return the number of errors and the position of the error in the row*/
241 void getErrorPositions(MPE_FEC_FRAME * mff, u32 row, u32 * errPositions);
242 
243 void setColRS( MPE_FEC_FRAME * mff, u32 offset, u8 * pds, u32 length );
244 void getColRS(MPE_FEC_FRAME * mff, u32 offset, u8 * pds, u32 length);
245 void setIpDatagram(MPE_FEC_FRAME * mff,u32 offset, u8 * dgram, u32 length );
246 
247 void setErrorIndicator(u32 * data , u32 offset1, u32 length  );
248 void resetMFF(MPE_FEC_FRAME * mff) ;
249 u32  getErrasurePositions( MPE_FEC_FRAME *mff , u32 row, u32 *errasures);
250 
251 void decode_fec(MPE_FEC_FRAME * mff);
252 
253 
254 // Descriptor tag space/scope...
255 typedef enum {
256 	MPEG, DVB_SI,
257 	DSMCC_STREAM, DSMCC_CAROUSEL, DSMCC_INT_UNT, MHP_AIT, TVA_RNT
258 } DTAG_SCOPE;
259 
260 void descriptor_PRIVATE (u8 *b, DTAG_SCOPE tag_scope, GF_List * descriptors );
261 
262 
263 #endif //GPAC_DISABLE_MPEG2TS
264 
265 #endif	//_GF_DVB_MPE_DEV_H_
266