]> git.sesse.net Git - vlc/blob - include/input.h
Encore un commit venu tout droit des abysses de l'enfer, d�sol� pour
[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 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.
69               */
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 */
73
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 */
78
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). */
85 } pes_packet_t;
86
87 /*****************************************************************************
88  * psi_section_t
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
94 {
95     byte_t        buffer[PSI_SECTION_SIZE];
96
97     boolean_t     b_running_section;   /* Is there a section being decoded ? */
98
99     u16 i_length;
100     u16 i_current_position;
101 } psi_section_t;
102
103
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
111 {
112     u16                     i_id;           /* stream ID, PID for TS streams */
113     u8                      i_type;                           /* stream type */
114
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
119      * PCR presence */
120     boolean_t               b_psi;  /* does the stream have to be handled by the
121                                                                 PSI decoder ? */
122     /* Markers */
123     int                     i_continuity_counter;
124     boolean_t               b_discontinuity;
125     boolean_t               b_random;
126
127     /* PES packets */
128     pes_packet_t *          p_pes_packet;
129                                       /* current PES packet we are gathering */
130
131     /* PSI packets */
132     psi_section_t *         p_psi_section;          /* idem for a PSI stream */
133
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
138                                         * casts. */
139
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 ? */
146
147 #ifdef STATS
148     /* Stats */
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 */
154 #endif
155 } es_descriptor_t;
156
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 */
162
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 */
170
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  *****************************************************************************/
177 typedef struct
178 {
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 */
184
185     int i_es_number;
186     es_descriptor_t **      ap_es;                /* array of pointers to ES */
187
188 #ifdef DVB_EXTENSIONS
189     /* Service Descriptor (program name) */
190     u8                      i_srv_type;
191     char*                   psz_srv_name;
192 #endif
193
194     /* XXX?? target background grid descriptor ? */
195     /* XXX?? video window descriptor ? */
196     /* XXX?? ISO 639 language descriptor ? */
197
198 #ifdef STATS
199     /* Stats */
200     /* XXX?? ...stats */
201 #endif
202 } pgrm_descriptor_t;
203
204 /*****************************************************************************
205  * pcr_descriptor_t
206  *****************************************************************************
207  * Contains informations used to synchronise the decoder with the server
208  *****************************************************************************/
209
210 typedef struct pcr_descriptor_struct
211 {
212     /* system_date = PTS_date + delta_pcr + delta_absolute */
213     mtime_t                 delta_pcr;
214     mtime_t                 delta_absolute;
215
216     mtime_t                 last_pcr;
217
218     u32                     i_synchro_state;
219     count_t                 c_average_count;
220                            /* counter used to compute dynamic average values */
221 } pcr_descriptor_t;
222
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  *****************************************************************************/
229 typedef struct
230 {
231     u16                     i_stream_id;                        /* stream id */
232
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 */
240
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 */
247
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 */
255
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 */
259
260 #ifdef STATS
261     /* Stats */
262     /* XXX?? ...stats */
263 #endif
264 } stream_descriptor_t;
265
266 /*****************************************************************************
267  * input_netlist_t
268  *****************************************************************************/
269 typedef struct
270 {
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 */
276
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 */
281
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
287     int                     i_ts_index;
288 #else
289     int                     i_ts_start, i_ts_end;
290 #endif
291 #ifdef INPUT_LIFO_PES_NETLIST
292     int                     i_pes_index;
293 #else
294     int                     i_pes_start, i_pes_end;
295 #endif
296 } input_netlist_t;
297
298
299
300 /*****************************************************************************
301  * input_thread_t
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  *****************************************************************************/
308
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,
312                                    size_t i_count );
313 typedef void (input_close_t)    ( p_input_thread_t p_input );
314
315 /* Structure */
316 typedef struct input_thread_s
317 {
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 */
325
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 */
335
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 */
340
341     /* List of streams to demux */
342     es_descriptor_t *       pp_selected_es[INPUT_MAX_SELECTED_ES];
343
344     /* Netlists */
345     input_netlist_t         netlist;                            /* see above */
346
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 */
350
351 #ifdef STATS
352     /* Statistics */
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 */
358 #endif
359 } input_thread_t;
360
361 /* Input methods */
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 */
368
369 /*****************************************************************************
370  * Prototypes
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 );
376
377
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 );