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 GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Constants related to input
26 *****************************************************************************/
27 #define TS_PACKET_SIZE 188 /* size of a TS packet */
28 #define PES_HEADER_SIZE 14 /* size of the first part of a PES header */
29 #define PSI_SECTION_SIZE 4096 /* Maximum size of a PSI section */
31 /*****************************************************************************
33 *****************************************************************************
34 * Describe a TS packet.
35 *****************************************************************************/
36 typedef struct ts_packet_s
38 /* Nothing before this line, the code relies on that */
39 byte_t buffer[TS_PACKET_SIZE]; /* raw TS data packet */
41 /* Decoders information */
42 unsigned int i_payload_start;
43 /* start of the PES payload in this packet */
44 unsigned int i_payload_end; /* guess ? :-) */
46 /* Used to chain the TS packets that carry data for a same PES or PSI */
47 struct ts_packet_s * p_prev_ts;
48 struct ts_packet_s * p_next_ts;
51 /*****************************************************************************
53 *****************************************************************************
54 * Describes an PES packet, with its properties, and pointers to the TS packets
56 *****************************************************************************/
57 typedef struct pes_packet_s
60 boolean_t b_data_loss; /* The previous (at least) PES packet
61 * has been lost. The decoders will have to find a way to recover. */
62 boolean_t b_data_alignment; /* used to find the beginning of a
63 * video or audio unit */
64 boolean_t b_has_pts; /* is the following field set ? */
65 mtime_t i_pts; /* the PTS for this packet (if set above) */
66 boolean_t b_random_access;
67 /* if TRUE, in the payload of this packet, there is the first byte
68 * of a video sequence header, or the first byte of an audio frame.
70 u8 i_stream_id; /* payload type and id */
71 int i_pes_size; /* size of the current PES packet */
72 int i_ts_packets;/* number of TS packets in this PES */
74 /* Demultiplexer environment */
75 boolean_t b_discard_payload; /* is the packet messed up ? */
76 byte_t * p_pes_header; /* pointer to the PES header */
77 byte_t * p_pes_header_save; /* temporary buffer */
79 /* Pointers to TS packets (TS packets are then linked by the p_prev_ts and
80 p_next_ts fields of the ts_packet_t struct) */
81 ts_packet_t * p_first_ts; /* The first TS packet containing this
82 * PES (used by decoders). */
83 ts_packet_t * p_last_ts; /* The last TS packet gathered at present
84 * (used by the demultiplexer). */
87 /*****************************************************************************
89 *****************************************************************************
90 * Describes a PSI section. Beware, it doesn't contain pointers to the TS
91 * packets that contain it as for a PES, but the data themselves
92 *****************************************************************************/
93 typedef struct psi_section_s
95 byte_t buffer[PSI_SECTION_SIZE];
97 boolean_t b_running_section; /* Is there a section being decoded ? */
100 u16 i_current_position;
104 /*****************************************************************************
105 * es_descriptor_t: elementary stream descriptor
106 *****************************************************************************
107 * Describes an elementary stream, and includes fields required to handle and
108 * demultiplex this elementary stream.
109 *****************************************************************************/
110 typedef struct es_descriptor_t
112 u16 i_id; /* stream ID, PID for TS streams */
113 u8 i_type; /* stream type */
115 boolean_t b_pcr; /* does the stream include a PCR ? */
116 /* XXX?? b_pcr will be replaced by something else: since a PCR can't be shared
117 * between several ES, we will probably store the PCR fields directly here,
118 * and one of those fields will probably (again) be used as a test of the
120 boolean_t b_psi; /* does the stream have to be handled by the
123 int i_continuity_counter;
124 boolean_t b_discontinuity;
128 pes_packet_t * p_pes_packet;
129 /* current PES packet we are gathering */
132 psi_section_t * p_psi_section; /* idem for a PSI stream */
134 /* Decoder informations */
135 void * p_dec; /* p_dec is void *, since we don't know a
136 * priori whether it is adec_thread_t or
137 * vdec_thread_t. We will use explicit
140 /* XXX?? video stream descriptor ? */
141 /* XXX?? audio stream descriptor ? */
142 /* XXX?? hierarchy descriptor ? */
143 /* XXX?? target background grid descriptor ? */
144 /* XXX?? video window descriptor ? */
145 /* XXX?? ISO 639 language descriptor ? */
149 count_t c_bytes; /* total bytes read */
150 count_t c_payload_bytes;/* total of payload usefull bytes */
151 count_t c_packets; /* total packets read */
152 count_t c_invalid_packets; /* invalid packets read */
153 /* XXX?? ... other stats */
157 /* Special PID values - note that the PID is only on 13 bits, and that values
158 * greater than 0x1fff have no meaning in a stream */
159 #define PROGRAM_ASSOCIATION_TABLE_PID 0x0000
160 #define CONDITIONNAL_ACCESS_TABLE_PID 0x0001 /* not used */
161 #define EMPTY_PID 0xffff /* empty record in a table */
163 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers */
164 #define MPEG1_VIDEO_ES 0x01
165 #define MPEG2_VIDEO_ES 0x02
166 #define MPEG1_AUDIO_ES 0x03
167 #define MPEG2_AUDIO_ES 0x04
168 #define AC3_AUDIO_ES 0x81
169 #define DVD_SPU_ES 0x82 /* 0x82 might violate the norm */
171 /*****************************************************************************
172 * program_descriptor_t
173 *****************************************************************************
174 * Describes a program and list associated elementary streams. It is build by
175 * the PSI decoder upon the informations carried in program map sections
176 *****************************************************************************/
179 /* Program characteristics */
180 u16 i_number; /* program number */
181 u8 i_version; /* version number */
182 boolean_t b_is_ok; /* Is the description up to date ?*/
183 u16 i_pcr_pid; /* PCR ES */
186 es_descriptor_t ** ap_es; /* array of pointers to ES */
188 #ifdef DVB_EXTENSIONS
189 /* Service Descriptor (program name) */
194 /* XXX?? target background grid descriptor ? */
195 /* XXX?? video window descriptor ? */
196 /* XXX?? ISO 639 language descriptor ? */
204 /*****************************************************************************
206 *****************************************************************************
207 * Contains informations used to synchronise the decoder with the server
208 *****************************************************************************/
210 typedef struct pcr_descriptor_struct
212 /* system_date = PTS_date + delta_pcr + delta_absolute */
214 mtime_t delta_absolute;
219 count_t c_average_count;
220 /* counter used to compute dynamic average values */
223 /*****************************************************************************
224 * stream_descriptor_t
225 *****************************************************************************
226 * Describes a transport stream and list its associated programs. Build upon
227 * the informations carried in program association sections
228 *****************************************************************************/
231 u16 i_stream_id; /* stream id */
233 /* Program Association Table status */
234 u8 i_PAT_version; /* version number */
235 boolean_t b_is_PAT_complete; /* Is the PAT complete ?*/
236 u8 i_known_PAT_sections;
237 /* Number of section we received so far */
238 byte_t a_known_PAT_sections[32];
239 /* Already received sections */
241 /* Program Map Table status */
242 boolean_t b_is_PMT_complete; /* Is the PMT complete ?*/
243 u8 i_known_PMT_sections;
244 /* Number of section we received so far */
245 byte_t a_known_PMT_sections[32];
246 /* Already received sections */
248 /* Service Description Table status */
249 u8 i_SDT_version; /* version number */
250 boolean_t b_is_SDT_complete; /* Is the SDT complete ?*/
251 u8 i_known_SDT_sections;
252 /* Number of section we received so far */
253 byte_t a_known_SDT_sections[32];
254 /* Already received sections */
256 /* Programs description */
257 int i_pgrm_number; /* Number of program number we have */
258 pgrm_descriptor_t ** ap_programs; /* Array of pointers to pgrm */
264 } stream_descriptor_t;
266 /*****************************************************************************
268 *****************************************************************************/
271 vlc_mutex_t lock; /* netlist modification lock */
272 struct iovec p_ts_free[INPUT_MAX_TS + INPUT_TS_READ_ONCE];
273 /* FIFO or LIFO of free TS packets */
274 ts_packet_t * p_ts_packets;
275 /* pointer to the first TS packet we allocated */
277 pes_packet_t * p_pes_free[INPUT_MAX_PES + 1];
278 /* FIFO or LIFO of free PES packets */
279 pes_packet_t * p_pes_packets;
280 /* pointer to the first PES packet we allocated */
282 /* To use the efficiency of the scatter/gather IO operations. We implemented
283 * it in 2 ways, as we don't know yet which one is better : as a FIFO (code
284 * simplier) or as a LIFO stack (when we doesn't care of the ordering, this
285 * allow to drastically improve the cache performance) */
286 #ifdef INPUT_LIFO_TS_NETLIST
289 int i_ts_start, i_ts_end;
291 #ifdef INPUT_LIFO_PES_NETLIST
294 int i_pes_start, i_pes_end;
300 /*****************************************************************************
302 *****************************************************************************
303 * This structure includes all the local static variables of an input thread,
304 * including the netlist and the ES descriptors
305 * Note that p_es must be defined as a static table, otherwise we would have to
306 * update all reference to it each time the table would be reallocated
307 *****************************************************************************/
309 /* Function pointers used in structure */
310 typedef int (input_open_t) ( p_input_thread_t p_input );
311 typedef int (input_read_t) ( p_input_thread_t p_input, const struct iovec *p_vector,
313 typedef void (input_close_t) ( p_input_thread_t p_input );
316 typedef struct input_thread_s
318 /* Thread properties and locks */
319 boolean_t b_die; /* 'die' flag */
320 boolean_t b_error; /* deadlock */
321 vlc_thread_t thread_id; /* id for thread functions */
322 vlc_mutex_t programs_lock; /* programs modification lock */
323 vlc_mutex_t es_lock; /* es modification lock */
324 int * pi_status; /* temporary status flag */
326 /* Input method description */
327 int i_method; /* input method */
328 int i_handle; /* file/socket descriptor */
329 char * psz_source; /* source */
330 int i_port; /* port number */
331 int i_vlan; /* id for vlan method */
332 input_open_t * p_Open; /* opener of the method */
333 input_read_t * p_Read; /* reading function */
334 input_close_t * p_Close; /* destroying function */
336 /* General stream description */
337 stream_descriptor_t * p_stream; /* PAT tables */
338 es_descriptor_t p_es[INPUT_MAX_ES];/* carried elementary streams */
339 pcr_descriptor_t * p_pcr; /* PCR struct used for synchronisation */
341 /* List of streams to demux */
342 es_descriptor_t * pp_selected_es[INPUT_MAX_SELECTED_ES];
345 input_netlist_t netlist; /* see above */
347 /* Default settings for spawned decoders */
348 p_aout_thread_t p_aout; /* audio output thread structure */
349 p_vout_thread_t p_vout; /* video output thread */
353 count_t c_loops; /* number of loops */
354 count_t c_bytes; /* bytes read */
355 count_t c_payload_bytes; /* payload useful bytes */
356 count_t c_packets_read; /* packets read */
357 count_t c_packets_trashed; /* trashed packets */
362 #define INPUT_METHOD_NONE 0 /* input thread is inactive */
363 #define INPUT_METHOD_TS_FILE 10 /* TS stream is read from a file */
364 #define INPUT_METHOD_TS_UCAST 20 /* TS UDP unicast */
365 #define INPUT_METHOD_TS_MCAST 21 /* TS UDP multicast */
366 #define INPUT_METHOD_TS_BCAST 22 /* TS UDP broadcast */
367 #define INPUT_METHOD_TS_VLAN_BCAST 32 /* TS UDP broadcast with VLANs */
369 /*****************************************************************************
371 *****************************************************************************/
372 input_thread_t *input_CreateThread ( int i_method, char *psz_source, int i_port,
373 int i_vlan, p_vout_thread_t p_vout,
374 p_aout_thread_t p_aout, int *pi_status );
375 void input_DestroyThread ( input_thread_t *p_input, int *pi_status );
378 int input_OpenAudioStream ( input_thread_t *p_input, int i_pid );
379 void input_CloseAudioStream ( input_thread_t *p_input, int i_pid );
380 int input_OpenVideoStream ( input_thread_t *p_input, int i_pid );
381 void input_CloseVideoStream ( input_thread_t *p_input, int i_pid );