]> git.sesse.net Git - vlc/blob - include/input.h
Bon finalement puisque tout le monde dort, je l'ai fait :)
[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 GNU
16  * General Public License for more details.
17  *
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  *****************************************************************************/
23
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 */
30
31 /*****************************************************************************
32  * ts_packet_t
33  *****************************************************************************
34  * Describe a TS packet.
35  *****************************************************************************/
36 typedef struct ts_packet_s
37 {
38     /* Nothing before this line, the code relies on that */
39     byte_t                  buffer[TS_PACKET_SIZE];    /* raw TS data packet */
40
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 ? :-) */
45
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;
49 } ts_packet_t;
50
51 /*****************************************************************************
52  * pes_packet_t
53  *****************************************************************************
54  * Describes an PES packet, with its properties, and pointers to the TS packets
55  * containing it.
56  *****************************************************************************/
57 typedef struct pes_packet_s
58 {
59     /* PES properties */
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
63                                                 * a 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.
69              */
70     u8                      i_stream_id;              /* payload type and id */
71     int                     i_pes_size;    /* size of the current PES packet */
72     int                     i_pes_real_size;      /* real size of the current
73                                                    * PES packet, ie. the one
74                                                    * announced in the header */
75     int                     i_ts_packets;/* number of TS packets in this PES */
76
77     /* Demultiplexer environment */
78     boolean_t               b_discard_payload;  /* is the packet messed up ? */
79     boolean_t               b_already_parsed;     /* was it already parsed ? */
80     byte_t *                p_pes_header;       /* pointer to the PES header */
81     byte_t *                p_pes_header_save;           /* temporary buffer */
82
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). */
89 } pes_packet_t;
90
91 /*****************************************************************************
92  * psi_section_t
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
98 {
99     byte_t        buffer[PSI_SECTION_SIZE];
100
101     boolean_t     b_running_section;   /* Is there a section being decoded ? */
102
103     u16 i_length;
104     u16 i_current_position;
105 } psi_section_t;
106
107
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
115 {
116     u16                     i_id;           /* stream ID, PID for TS streams */
117     u8                      i_type;                           /* stream type */
118
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
123      * PCR presence */
124     boolean_t               b_psi;  /* does the stream have to be handled by the
125                                                                 PSI decoder ? */
126     /* Markers */
127     int                     i_continuity_counter;
128     boolean_t               b_discontinuity;
129     boolean_t               b_random;
130
131     /* PES packets */
132     pes_packet_t *          p_pes_packet;
133                                       /* current PES packet we are gathering */
134
135     /* PSI packets */
136     psi_section_t *         p_psi_section;          /* idem for a PSI stream */
137
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
142                                         * casts. */
143
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 ? */
150
151 #ifdef STATS
152     /* Stats */
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 */
158 #endif
159 } es_descriptor_t;
160
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 */
166
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
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  *****************************************************************************/
181 typedef struct
182 {
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 */
188
189     int i_es_number;
190     es_descriptor_t **      ap_es;                /* array of pointers to ES */
191
192 #ifdef DVB_EXTENSIONS
193     /* Service Descriptor (program name) */
194     u8                      i_srv_type;
195     char*                   psz_srv_name;
196 #endif
197
198     /* XXX?? target background grid descriptor ? */
199     /* XXX?? video window descriptor ? */
200     /* XXX?? ISO 639 language descriptor ? */
201
202 #ifdef STATS
203     /* Stats */
204     /* XXX?? ...stats */
205 #endif
206 } pgrm_descriptor_t;
207
208 /*****************************************************************************
209  * pcr_descriptor_t
210  *****************************************************************************
211  * Contains informations used to synchronise the decoder with the server
212  *****************************************************************************/
213
214 typedef struct pcr_descriptor_struct
215 {
216     /* system_date = PTS_date + delta_pcr + delta_absolute */
217     mtime_t                 delta_pcr;
218     mtime_t                 delta_absolute;
219
220     mtime_t                 last_pcr;
221
222     u32                     i_synchro_state;
223     count_t                 c_average_count;
224                            /* counter used to compute dynamic average values */
225 } pcr_descriptor_t;
226
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  *****************************************************************************/
233 typedef struct
234 {
235     u16                     i_stream_id;                        /* stream id */
236
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 */
244
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 */
251
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 */
259
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 */
263
264 #ifdef STATS
265     /* Stats */
266     /* XXX?? ...stats */
267 #endif
268 } stream_descriptor_t;
269
270 /*****************************************************************************
271  * input_netlist_t
272  *****************************************************************************/
273 typedef struct
274 {
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 */
280
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 */
285
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
291     int                     i_ts_index;
292 #else
293     int                     i_ts_start, i_ts_end;
294 #endif
295 #ifdef INPUT_LIFO_PES_NETLIST
296     int                     i_pes_index;
297 #else
298     int                     i_pes_start, i_pes_end;
299 #endif
300 } input_netlist_t;
301
302
303
304 /*****************************************************************************
305  * input_thread_t
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  *****************************************************************************/
312
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,
316                                    size_t i_count );
317 typedef void (input_close_t)    ( p_input_thread_t p_input );
318
319 /* Structure */
320 typedef struct input_thread_s
321 {
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 */
329
330     /* Input method description */
331     int                         i_method;                    /* input method */
332     int                         i_handle;          /* file/socket descriptor */
333     char *                      psz_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 */
339
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 */
344
345     /* List of streams to demux */
346     es_descriptor_t *       pp_selected_es[INPUT_MAX_SELECTED_ES];
347
348     /* Netlists */
349     input_netlist_t         netlist;                            /* see above */
350
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 */
354
355 #ifdef STATS
356     /* Statistics */
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 */
362 #endif
363 } input_thread_t;
364
365 /* Input methods */
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 */
372
373 /*****************************************************************************
374  * Prototypes
375  *****************************************************************************/
376 input_thread_t *input_CreateThread      ( int i_method, char *psz_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 );
380
381
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 );