1 /*****************************************************************************
2 * input.h: input thread interface
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 * Vincent Seguin <seguin@via.ecp.fr>
8 * BenoƮt Steiner <benny@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
26 * Constants related to input
27 *****************************************************************************/
28 #define TS_PACKET_SIZE 188 /* size of a TS packet */
29 #define PES_HEADER_SIZE 14 /* size of the first part of a PES header */
30 #define PSI_SECTION_SIZE 4096 /* Maximum size of a PSI section */
32 /*****************************************************************************
34 *****************************************************************************
35 * Describe a TS packet.
36 *****************************************************************************/
37 typedef struct ts_packet_s
39 /* Nothing before this line, the code relies on that */
40 byte_t buffer[TS_PACKET_SIZE]; /* raw TS data packet */
42 /* Decoders information */
43 unsigned int i_payload_start;
44 /* start of the PES payload in this packet */
45 unsigned int i_payload_end; /* guess ? :-) */
47 /* Used to chain the TS packets that carry data for a same PES or PSI */
48 struct ts_packet_s * p_prev_ts;
49 struct ts_packet_s * p_next_ts;
52 /*****************************************************************************
54 *****************************************************************************
55 * Describes an PES packet, with its properties, and pointers to the TS packets
57 *****************************************************************************/
58 typedef struct pes_packet_s
61 boolean_t b_data_loss; /* The previous (at least) PES packet
62 * has been lost. The decoders will have to find a way to recover. */
63 boolean_t b_data_alignment; /* used to find the beginning of
64 * a video or audio unit */
65 boolean_t b_has_pts; /* is the following field set ? */
66 mtime_t i_pts; /* the PTS for this packet (if set above) */
67 boolean_t b_random_access;
68 /* if TRUE, in the payload of this packet, there is the first byte
69 * of a video sequence header, or the first byte of an audio frame.
71 u8 i_stream_id; /* payload type and id */
72 int i_pes_size; /* size of the current PES packet */
73 int i_pes_real_size; /* real size of the current
74 * PES packet, ie. the one
75 * announced in the header */
76 int i_ts_packets;/* number of TS packets in this PES */
78 /* Demultiplexer environment */
79 boolean_t b_discard_payload; /* is the packet messed up ? */
80 byte_t * p_pes_header; /* pointer to the PES header */
81 byte_t * p_pes_header_save; /* temporary buffer */
83 /* Pointers to TS packets (TS packets are then linked by the p_prev_ts and
84 p_next_ts fields of the ts_packet_t struct) */
85 ts_packet_t * p_first_ts; /* The first TS packet containing this
86 * PES (used by decoders). */
87 ts_packet_t * p_last_ts; /* The last TS packet gathered at present
88 * (used by the demultiplexer). */
91 /*****************************************************************************
93 *****************************************************************************
94 * Describes a PSI section. Beware, it doesn't contain pointers to the TS
95 * packets that contain it as for a PES, but the data themselves
96 *****************************************************************************/
97 typedef struct psi_section_s
99 byte_t buffer[PSI_SECTION_SIZE];
101 boolean_t b_running_section; /* Is there a section being decoded ? */
104 u16 i_current_position;
108 /*****************************************************************************
109 * es_descriptor_t: elementary stream descriptor
110 *****************************************************************************
111 * Describes an elementary stream, and includes fields required to handle and
112 * demultiplex this elementary stream.
113 *****************************************************************************/
114 typedef struct es_descriptor_t
116 u16 i_id; /* stream ID, PID for TS streams */
117 u8 i_type; /* stream type */
119 boolean_t b_pcr; /* does the stream include a PCR ? */
120 /* XXX?? b_pcr will be replaced by something else: since a PCR can't be shared
121 * between several ES, we will probably store the PCR fields directly here,
122 * and one of those fields will probably (again) be used as a test of the
124 boolean_t b_psi; /* does the stream have to be handled by the
127 int i_continuity_counter;
128 boolean_t b_discontinuity;
132 pes_packet_t * p_pes_packet;
133 /* current PES packet we are gathering */
136 psi_section_t * p_psi_section; /* idem for a PSI stream */
138 /* Decoder informations */
139 void * p_dec; /* p_dec is void *, since we don't know a
140 * priori whether it is adec_thread_t or
141 * vdec_thread_t. We will use explicit
144 /* XXX?? video stream descriptor ? */
145 /* XXX?? audio stream descriptor ? */
146 /* XXX?? hierarchy descriptor ? */
147 /* XXX?? target background grid descriptor ? */
148 /* XXX?? video window descriptor ? */
149 /* XXX?? ISO 639 language descriptor ? */
153 count_t c_bytes; /* total bytes read */
154 count_t c_payload_bytes;/* total of payload useful bytes */
155 count_t c_packets; /* total packets read */
156 count_t c_invalid_packets; /* invalid packets read */
157 /* XXX?? ... other stats */
161 /* Special PID values - note that the PID is only on 13 bits, and that values
162 * greater than 0x1fff have no meaning in a stream */
163 #define PROGRAM_ASSOCIATION_TABLE_PID 0x0000
164 #define CONDITIONNAL_ACCESS_TABLE_PID 0x0001 /* not used */
165 #define EMPTY_PID 0xffff /* empty record in a table */
167 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers */
168 #define MPEG1_VIDEO_ES 0x01
169 #define MPEG2_VIDEO_ES 0x02
170 #define MPEG1_AUDIO_ES 0x03
171 #define MPEG2_AUDIO_ES 0x04
172 #define AC3_AUDIO_ES 0x81
173 #define DVD_SPU_ES 0x82 /* 0x82 might violate the norm */
174 #define LPCM_AUDIO_ES 0x83
175 /*****************************************************************************
176 * program_descriptor_t
177 *****************************************************************************
178 * Describes a program and list associated elementary streams. It is build by
179 * the PSI decoder upon the informations carried in program map sections
180 *****************************************************************************/
183 /* Program characteristics */
184 u16 i_number; /* program number */
185 u8 i_version; /* version number */
186 boolean_t b_is_ok; /* Is the description up to date ?*/
187 u16 i_pcr_pid; /* PCR ES */
190 es_descriptor_t ** ap_es; /* array of pointers to ES */
192 #ifdef DVB_EXTENSIONS
193 /* Service Descriptor (program name) */
198 /* XXX?? target background grid descriptor ? */
199 /* XXX?? video window descriptor ? */
200 /* XXX?? ISO 639 language descriptor ? */
208 /*****************************************************************************
210 *****************************************************************************
211 * Contains informations used to synchronise the decoder with the server
212 *****************************************************************************/
214 typedef struct pcr_descriptor_struct
216 /* system_date = PTS_date + delta_pcr + delta_absolute */
218 mtime_t delta_absolute;
223 count_t c_average_count;
224 /* counter used to compute dynamic average values */
227 /*****************************************************************************
228 * stream_descriptor_t
229 *****************************************************************************
230 * Describes a transport stream and list its associated programs. Build upon
231 * the informations carried in program association sections
232 *****************************************************************************/
235 u16 i_stream_id; /* stream id */
237 /* Program Association Table status */
238 u8 i_PAT_version; /* version number */
239 boolean_t b_is_PAT_complete; /* Is the PAT complete ?*/
240 u8 i_known_PAT_sections;
241 /* Number of section we received so far */
242 byte_t a_known_PAT_sections[32];
243 /* Already received sections */
245 /* Program Map Table status */
246 boolean_t b_is_PMT_complete; /* Is the PMT complete ?*/
247 u8 i_known_PMT_sections;
248 /* Number of section we received so far */
249 byte_t a_known_PMT_sections[32];
250 /* Already received sections */
252 /* Service Description Table status */
253 u8 i_SDT_version; /* version number */
254 boolean_t b_is_SDT_complete; /* Is the SDT complete ?*/
255 u8 i_known_SDT_sections;
256 /* Number of section we received so far */
257 byte_t a_known_SDT_sections[32];
258 /* Already received sections */
260 /* Programs description */
261 int i_pgrm_number; /* Number of program number we have */
262 pgrm_descriptor_t ** ap_programs; /* Array of pointers to pgrm */
268 } stream_descriptor_t;
270 /*****************************************************************************
272 *****************************************************************************/
275 vlc_mutex_t lock; /* netlist modification lock */
276 struct iovec p_ts_free[INPUT_MAX_TS + INPUT_TS_READ_ONCE];
277 /* FIFO or LIFO of free TS packets */
278 ts_packet_t * p_ts_packets;
279 /* pointer to the first TS packet we allocated */
281 pes_packet_t * p_pes_free[INPUT_MAX_PES + 1];
282 /* FIFO or LIFO of free PES packets */
283 pes_packet_t * p_pes_packets;
284 /* pointer to the first PES packet we allocated */
286 /* To use the efficiency of the scatter/gather IO operations. We implemented
287 * it in 2 ways, as we don't know yet which one is better : as a FIFO (code
288 * simplier) or as a LIFO stack (when we doesn't care of the ordering, this
289 * allow to drastically improve the cache performance) */
290 #ifdef INPUT_LIFO_TS_NETLIST
293 int i_ts_start, i_ts_end;
295 #ifdef INPUT_LIFO_PES_NETLIST
298 int i_pes_start, i_pes_end;
304 /*****************************************************************************
306 *****************************************************************************
307 * This structure includes all the local static variables of an input thread,
308 * including the netlist and the ES descriptors
309 * Note that p_es must be defined as a static table, otherwise we would have to
310 * update all reference to it each time the table would be reallocated
311 *****************************************************************************/
313 /* Function pointers used in structure */
314 typedef int (input_open_t) ( p_input_thread_t p_input );
315 typedef int (input_read_t) ( p_input_thread_t p_input, const struct iovec *p_vector,
317 typedef void (input_close_t) ( p_input_thread_t p_input );
320 typedef struct input_thread_s
322 /* Thread properties and locks */
323 boolean_t b_die; /* 'die' flag */
324 boolean_t b_error; /* deadlock */
325 vlc_thread_t thread_id; /* id for thread functions */
326 vlc_mutex_t programs_lock; /* programs modification lock */
327 vlc_mutex_t es_lock; /* es modification lock */
328 int * pi_status; /* temporary status flag */
330 /* Input method description */
331 int i_method; /* input method */
332 int i_handle; /* file/socket descriptor */
333 char * p_source; /* source */
334 int i_port; /* port number */
335 int i_vlan; /* id for vlan method */
336 input_open_t * p_Open; /* opener of the method */
337 input_read_t * p_Read; /* reading function */
338 input_close_t * p_Close; /* destroying function */
340 /* General stream description */
341 stream_descriptor_t * p_stream; /* PAT tables */
342 es_descriptor_t p_es[INPUT_MAX_ES];/* carried elementary streams */
343 pcr_descriptor_t * p_pcr; /* PCR struct used for synchronisation */
345 /* List of streams to demux */
346 es_descriptor_t * pp_selected_es[INPUT_MAX_SELECTED_ES];
349 input_netlist_t netlist; /* see above */
351 /* Default settings for spawned decoders */
352 p_aout_thread_t p_aout; /* audio output thread structure */
353 p_vout_thread_t p_vout; /* video output thread */
357 count_t c_loops; /* number of loops */
358 count_t c_bytes; /* bytes read */
359 count_t c_payload_bytes; /* payload useful bytes */
360 count_t c_packets_read; /* packets read */
361 count_t c_packets_trashed; /* trashed packets */
366 #define INPUT_METHOD_NONE 0 /* input thread is inactive */
367 #define INPUT_METHOD_TS_FILE 10 /* TS stream is read from a file */
368 #define INPUT_METHOD_TS_UCAST 20 /* TS UDP unicast */
369 #define INPUT_METHOD_TS_MCAST 21 /* TS UDP multicast */
370 #define INPUT_METHOD_TS_BCAST 22 /* TS UDP broadcast */
371 #define INPUT_METHOD_TS_VLAN_BCAST 32 /* TS UDP broadcast with VLANs */
373 /*****************************************************************************
375 *****************************************************************************/
376 input_thread_t *input_CreateThread ( int i_method, void *p_source, int i_port,
377 int i_vlan, p_vout_thread_t p_vout,
378 p_aout_thread_t p_aout, int *pi_status );
379 void input_DestroyThread ( input_thread_t *p_input, int *pi_status );
382 int input_OpenAudioStream ( input_thread_t *p_input, int i_pid );
383 void input_CloseAudioStream ( input_thread_t *p_input, int i_pid );
384 int input_OpenVideoStream ( input_thread_t *p_input, int i_pid );
385 void input_CloseVideoStream ( input_thread_t *p_input, int i_pid );