1 /*****************************************************************************
2 * input.h: input thread interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Constants related to input
25 *****************************************************************************/
26 #define TS_PACKET_SIZE 188 /* size of a TS packet */
27 #define PES_HEADER_SIZE 14 /* size of the first part of a PES header */
28 #define PSI_SECTION_SIZE 4096 /* Maximum size of a PSI section */
30 /*****************************************************************************
32 *****************************************************************************
33 * Describe a TS packet.
34 *****************************************************************************/
35 typedef struct ts_packet_s
37 /* Nothing before this line, the code relies on that */
38 byte_t buffer[TS_PACKET_SIZE]; /* raw TS data packet */
40 /* Decoders information */
41 unsigned int i_payload_start;
42 /* start of the PES payload in this packet */
43 unsigned int i_payload_end; /* guess ? :-) */
45 /* Used to chain the TS packets that carry data for a same PES or PSI */
46 struct ts_packet_s * p_prev_ts;
47 struct ts_packet_s * p_next_ts;
50 /*****************************************************************************
52 *****************************************************************************
53 * Describes an PES packet, with its properties, and pointers to the TS packets
55 *****************************************************************************/
56 typedef struct pes_packet_s
59 boolean_t b_data_loss; /* The previous (at least) PES packet
60 * has been lost. The decoders will have to find a way to recover. */
61 boolean_t b_data_alignment; /* used to find the beginning of
62 * a video or audio unit */
63 boolean_t b_has_pts; /* is the following field set ? */
64 mtime_t i_pts; /* the PTS for this packet (if set above) */
65 boolean_t b_random_access;
66 /* if TRUE, in the payload of this packet, there is the first byte
67 * of a video sequence header, or the first byte of an audio frame.
69 u8 i_stream_id; /* payload type and id */
70 int i_pes_size; /* size of the current PES packet */
71 int i_pes_real_size; /* real size of the current
72 * PES packet, ie. the one
73 * announced in the header */
74 int i_ts_packets;/* number of TS packets in this PES */
76 /* Demultiplexer environment */
77 boolean_t b_discard_payload; /* is the packet messed up ? */
78 byte_t * p_pes_header; /* pointer to the PES header */
79 byte_t * p_pes_header_save; /* temporary buffer */
81 /* Pointers to TS packets (TS packets are then linked by the p_prev_ts and
82 p_next_ts fields of the ts_packet_t struct) */
83 ts_packet_t * p_first_ts; /* The first TS packet containing this
84 * PES (used by decoders). */
85 ts_packet_t * p_last_ts; /* The last TS packet gathered at present
86 * (used by the demultiplexer). */
89 /*****************************************************************************
91 *****************************************************************************
92 * Describes a PSI section. Beware, it doesn't contain pointers to the TS
93 * packets that contain it as for a PES, but the data themselves
94 *****************************************************************************/
95 typedef struct psi_section_s
97 byte_t buffer[PSI_SECTION_SIZE];
99 boolean_t b_running_section; /* Is there a section being decoded ? */
102 u16 i_current_position;
106 /*****************************************************************************
107 * es_descriptor_t: elementary stream descriptor
108 *****************************************************************************
109 * Describes an elementary stream, and includes fields required to handle and
110 * demultiplex this elementary stream.
111 *****************************************************************************/
112 typedef struct es_descriptor_t
114 u16 i_id; /* stream ID, PID for TS streams */
115 u8 i_type; /* stream type */
117 boolean_t b_pcr; /* does the stream include a PCR ? */
118 /* XXX?? b_pcr will be replaced by something else: since a PCR can't be shared
119 * between several ES, we will probably store the PCR fields directly here,
120 * and one of those fields will probably (again) be used as a test of the
122 boolean_t b_psi; /* does the stream have to be handled by the
125 int i_continuity_counter;
126 boolean_t b_discontinuity;
130 pes_packet_t * p_pes_packet;
131 /* current PES packet we are gathering */
134 psi_section_t * p_psi_section; /* idem for a PSI stream */
136 /* Decoder informations */
137 void * p_dec; /* p_dec is void *, since we don't know a
138 * priori whether it is adec_thread_t or
139 * vdec_thread_t. We will use explicit
142 /* XXX?? video stream descriptor ? */
143 /* XXX?? audio stream descriptor ? */
144 /* XXX?? hierarchy descriptor ? */
145 /* XXX?? target background grid descriptor ? */
146 /* XXX?? video window descriptor ? */
147 /* XXX?? ISO 639 language descriptor ? */
151 count_t c_bytes; /* total bytes read */
152 count_t c_payload_bytes;/* total of payload useful bytes */
153 count_t c_packets; /* total packets read */
154 count_t c_invalid_packets; /* invalid packets read */
155 /* XXX?? ... other stats */
159 /* Special PID values - note that the PID is only on 13 bits, and that values
160 * greater than 0x1fff have no meaning in a stream */
161 #define PROGRAM_ASSOCIATION_TABLE_PID 0x0000
162 #define CONDITIONNAL_ACCESS_TABLE_PID 0x0001 /* not used */
163 #define EMPTY_PID 0xffff /* empty record in a table */
165 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers */
166 #define MPEG1_VIDEO_ES 0x01
167 #define MPEG2_VIDEO_ES 0x02
168 #define MPEG1_AUDIO_ES 0x03
169 #define MPEG2_AUDIO_ES 0x04
170 #define AC3_AUDIO_ES 0x81
171 #define DVD_SPU_ES 0x82 /* 0x82 might violate the norm */
172 #define LPCM_AUDIO_ES 0x83
173 /*****************************************************************************
174 * program_descriptor_t
175 *****************************************************************************
176 * Describes a program and list associated elementary streams. It is build by
177 * the PSI decoder upon the informations carried in program map sections
178 *****************************************************************************/
181 /* Program characteristics */
182 u16 i_number; /* program number */
183 u8 i_version; /* version number */
184 boolean_t b_is_ok; /* Is the description up to date ?*/
185 u16 i_pcr_pid; /* PCR ES */
188 es_descriptor_t ** ap_es; /* array of pointers to ES */
190 #ifdef DVB_EXTENSIONS
191 /* Service Descriptor (program name) */
196 /* XXX?? target background grid descriptor ? */
197 /* XXX?? video window descriptor ? */
198 /* XXX?? ISO 639 language descriptor ? */
206 /*****************************************************************************
208 *****************************************************************************
209 * Contains informations used to synchronise the decoder with the server
210 *****************************************************************************/
212 typedef struct pcr_descriptor_struct
214 /* system_date = PTS_date + delta_pcr + delta_absolute */
216 mtime_t delta_absolute;
221 count_t c_average_count;
222 /* counter used to compute dynamic average values */
225 /*****************************************************************************
226 * stream_descriptor_t
227 *****************************************************************************
228 * Describes a transport stream and list its associated programs. Build upon
229 * the informations carried in program association sections
230 *****************************************************************************/
233 u16 i_stream_id; /* stream id */
235 /* Program Association Table status */
236 u8 i_PAT_version; /* version number */
237 boolean_t b_is_PAT_complete; /* Is the PAT complete ?*/
238 u8 i_known_PAT_sections;
239 /* Number of section we received so far */
240 byte_t a_known_PAT_sections[32];
241 /* Already received sections */
243 /* Program Map Table status */
244 boolean_t b_is_PMT_complete; /* Is the PMT complete ?*/
245 u8 i_known_PMT_sections;
246 /* Number of section we received so far */
247 byte_t a_known_PMT_sections[32];
248 /* Already received sections */
250 /* Service Description Table status */
251 u8 i_SDT_version; /* version number */
252 boolean_t b_is_SDT_complete; /* Is the SDT complete ?*/
253 u8 i_known_SDT_sections;
254 /* Number of section we received so far */
255 byte_t a_known_SDT_sections[32];
256 /* Already received sections */
258 /* Programs description */
259 int i_pgrm_number; /* Number of program number we have */
260 pgrm_descriptor_t ** ap_programs; /* Array of pointers to pgrm */
266 } stream_descriptor_t;
268 /*****************************************************************************
270 *****************************************************************************/
273 vlc_mutex_t lock; /* netlist modification lock */
274 struct iovec p_ts_free[INPUT_MAX_TS + INPUT_TS_READ_ONCE];
275 /* FIFO or LIFO of free TS packets */
276 ts_packet_t * p_ts_packets;
277 /* pointer to the first TS packet we allocated */
279 pes_packet_t * p_pes_free[INPUT_MAX_PES + 1];
280 /* FIFO or LIFO of free PES packets */
281 pes_packet_t * p_pes_packets;
282 /* pointer to the first PES packet we allocated */
284 /* To use the efficiency of the scatter/gather IO operations. We implemented
285 * it in 2 ways, as we don't know yet which one is better : as a FIFO (code
286 * simplier) or as a LIFO stack (when we doesn't care of the ordering, this
287 * allow to drastically improve the cache performance) */
288 #ifdef INPUT_LIFO_TS_NETLIST
291 int i_ts_start, i_ts_end;
293 #ifdef INPUT_LIFO_PES_NETLIST
296 int i_pes_start, i_pes_end;
302 /*****************************************************************************
304 *****************************************************************************
305 * This structure includes all the local static variables of an input thread,
306 * including the netlist and the ES descriptors
307 * Note that p_es must be defined as a static table, otherwise we would have to
308 * update all reference to it each time the table would be reallocated
309 *****************************************************************************/
311 /* Function pointers used in structure */
312 typedef int (input_open_t) ( p_input_thread_t p_input );
313 typedef int (input_read_t) ( p_input_thread_t p_input, const struct iovec *p_vector,
315 typedef void (input_close_t) ( p_input_thread_t p_input );
318 typedef struct input_thread_s
320 /* Thread properties and locks */
321 boolean_t b_die; /* 'die' flag */
322 boolean_t b_error; /* deadlock */
323 vlc_thread_t thread_id; /* id for thread functions */
324 vlc_mutex_t programs_lock; /* programs modification lock */
325 vlc_mutex_t es_lock; /* es modification lock */
326 int * pi_status; /* temporary status flag */
328 /* Input method description */
329 int i_method; /* input method */
330 int i_handle; /* file/socket descriptor */
331 char * psz_source; /* source */
332 int i_port; /* port number */
333 int i_vlan; /* id for vlan method */
334 input_open_t * p_Open; /* opener of the method */
335 input_read_t * p_Read; /* reading function */
336 input_close_t * p_Close; /* destroying function */
338 /* General stream description */
339 stream_descriptor_t * p_stream; /* PAT tables */
340 es_descriptor_t p_es[INPUT_MAX_ES];/* carried elementary streams */
341 pcr_descriptor_t * p_pcr; /* PCR struct used for synchronisation */
343 /* List of streams to demux */
344 es_descriptor_t * pp_selected_es[INPUT_MAX_SELECTED_ES];
347 input_netlist_t netlist; /* see above */
349 /* Default settings for spawned decoders */
350 p_aout_thread_t p_aout; /* audio output thread structure */
351 p_vout_thread_t p_vout; /* video output thread */
355 count_t c_loops; /* number of loops */
356 count_t c_bytes; /* bytes read */
357 count_t c_payload_bytes; /* payload useful bytes */
358 count_t c_packets_read; /* packets read */
359 count_t c_packets_trashed; /* trashed packets */
364 #define INPUT_METHOD_NONE 0 /* input thread is inactive */
365 #define INPUT_METHOD_TS_FILE 10 /* TS stream is read from a file */
366 #define INPUT_METHOD_TS_UCAST 20 /* TS UDP unicast */
367 #define INPUT_METHOD_TS_MCAST 21 /* TS UDP multicast */
368 #define INPUT_METHOD_TS_BCAST 22 /* TS UDP broadcast */
369 #define INPUT_METHOD_TS_VLAN_BCAST 32 /* TS UDP broadcast with VLANs */
371 /*****************************************************************************
373 *****************************************************************************/
374 input_thread_t *input_CreateThread ( int i_method, char *psz_source, int i_port,
375 int i_vlan, p_vout_thread_t p_vout,
376 p_aout_thread_t p_aout, int *pi_status );
377 void input_DestroyThread ( input_thread_t *p_input, int *pi_status );
380 int input_OpenAudioStream ( input_thread_t *p_input, int i_pid );
381 void input_CloseAudioStream ( input_thread_t *p_input, int i_pid );
382 int input_OpenVideoStream ( input_thread_t *p_input, int i_pid );
383 void input_CloseVideoStream ( input_thread_t *p_input, int i_pid );