]> git.sesse.net Git - vlc/blob - include/input.h
. input fichier en ligne de commande (le reste a du p�ter, c'est pas
[vlc] / include / input.h
1 /*****************************************************************************
2  * input.h: input thread interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors:
7  *
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.
12  * 
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.
17  *
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  *****************************************************************************/
22
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 */
29
30 /*****************************************************************************
31  * ts_packet_t
32  *****************************************************************************
33  * Describe a TS packet.
34  *****************************************************************************/
35 typedef struct ts_packet_s
36 {
37     /* Nothing before this line, the code relies on that */
38     byte_t                  buffer[TS_PACKET_SIZE];    /* raw TS data packet */
39
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 ? :-) */
44
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;
48 } ts_packet_t;
49
50 /*****************************************************************************
51  * pes_packet_t
52  *****************************************************************************
53  * Describes an PES packet, with its properties, and pointers to the TS packets
54  * containing it.
55  *****************************************************************************/
56 typedef struct pes_packet_s
57 {
58     /* PES properties */
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.
68              */
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 */
75
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 */
80
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). */
87 } pes_packet_t;
88
89 /*****************************************************************************
90  * psi_section_t
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
96 {
97     byte_t        buffer[PSI_SECTION_SIZE];
98
99     boolean_t     b_running_section;   /* Is there a section being decoded ? */
100
101     u16 i_length;
102     u16 i_current_position;
103 } psi_section_t;
104
105
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
113 {
114     u16                     i_id;           /* stream ID, PID for TS streams */
115     u8                      i_type;                           /* stream type */
116
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
121      * PCR presence */
122     boolean_t               b_psi;  /* does the stream have to be handled by the
123                                                                 PSI decoder ? */
124     /* Markers */
125     int                     i_continuity_counter;
126     boolean_t               b_discontinuity;
127     boolean_t               b_random;
128
129     /* PES packets */
130     pes_packet_t *          p_pes_packet;
131                                       /* current PES packet we are gathering */
132
133     /* PSI packets */
134     psi_section_t *         p_psi_section;          /* idem for a PSI stream */
135
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
140                                         * casts. */
141
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 ? */
148
149 #ifdef STATS
150     /* Stats */
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 */
156 #endif
157 } es_descriptor_t;
158
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 */
164
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  *****************************************************************************/
179 typedef struct
180 {
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 */
186
187     int i_es_number;
188     es_descriptor_t **      ap_es;                /* array of pointers to ES */
189
190 #ifdef DVB_EXTENSIONS
191     /* Service Descriptor (program name) */
192     u8                      i_srv_type;
193     char*                   psz_srv_name;
194 #endif
195
196     /* XXX?? target background grid descriptor ? */
197     /* XXX?? video window descriptor ? */
198     /* XXX?? ISO 639 language descriptor ? */
199
200 #ifdef STATS
201     /* Stats */
202     /* XXX?? ...stats */
203 #endif
204 } pgrm_descriptor_t;
205
206 /*****************************************************************************
207  * pcr_descriptor_t
208  *****************************************************************************
209  * Contains informations used to synchronise the decoder with the server
210  *****************************************************************************/
211
212 typedef struct pcr_descriptor_struct
213 {
214     /* system_date = PTS_date + delta_pcr + delta_absolute */
215     mtime_t                 delta_pcr;
216     mtime_t                 delta_absolute;
217
218     mtime_t                 last_pcr;
219
220     u32                     i_synchro_state;
221     count_t                 c_average_count;
222                            /* counter used to compute dynamic average values */
223 } pcr_descriptor_t;
224
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  *****************************************************************************/
231 typedef struct
232 {
233     u16                     i_stream_id;                        /* stream id */
234
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 */
242
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 */
249
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 */
257
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 */
261
262 #ifdef STATS
263     /* Stats */
264     /* XXX?? ...stats */
265 #endif
266 } stream_descriptor_t;
267
268 /*****************************************************************************
269  * input_netlist_t
270  *****************************************************************************/
271 typedef struct
272 {
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 */
278
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 */
283
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
289     int                     i_ts_index;
290 #else
291     int                     i_ts_start, i_ts_end;
292 #endif
293 #ifdef INPUT_LIFO_PES_NETLIST
294     int                     i_pes_index;
295 #else
296     int                     i_pes_start, i_pes_end;
297 #endif
298 } input_netlist_t;
299
300
301
302 /*****************************************************************************
303  * input_thread_t
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  *****************************************************************************/
310
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,
314                                    size_t i_count );
315 typedef void (input_close_t)    ( p_input_thread_t p_input );
316
317 /* Structure */
318 typedef struct input_thread_s
319 {
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 */
327
328     /* Input method description */
329     int                         i_method;                    /* input method */
330     int                         i_handle;          /* file/socket descriptor */
331     char *                      p_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 */
337
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 */
342
343     /* List of streams to demux */
344     es_descriptor_t *       pp_selected_es[INPUT_MAX_SELECTED_ES];
345
346     /* Netlists */
347     input_netlist_t         netlist;                            /* see above */
348
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 */
352
353 #ifdef STATS
354     /* Statistics */
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 */
360 #endif
361 } input_thread_t;
362
363 /* Input methods */
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 */
370
371 /*****************************************************************************
372  * Prototypes
373  *****************************************************************************/
374 input_thread_t *input_CreateThread      ( int i_method, void *p_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 );
378
379
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 );