]> git.sesse.net Git - vlc/blob - include/input.h
- je ne ferai plus de commits d�biles � 5h du mat
[vlc] / include / input.h
1 /******************************************************************************
2  * input.h: input thread interface
3  * (c)1999 VideoLAN
4  ******************************************************************************/
5
6 /* needs : "netlist.h", "config.h", "mtime.h" */
7
8 /* ?? missing: 
9  *              tables version control */
10
11 /******************************************************************************
12  * External structures
13  ******************************************************************************
14  * These structures, required here only as pointers destinations, are declared 
15  * in other headers.
16  ******************************************************************************/
17 struct video_cfg_s;                         /* video configuration descriptor */
18 struct vout_thread_s;                                  /* video output thread */
19 struct stream_descriptor_s;                                     /* PSI tables */
20
21 /******************************************************************************
22  * Constants related to input
23  ******************************************************************************/
24 #define TS_PACKET_SIZE      188                        /* size of a TS packet */
25 #define PES_HEADER_SIZE     14      /* size of the first part of a PES header */
26 #define PSI_SECTION_SIZE    4096             /* Maximum size of a PSI section */
27
28 /******************************************************************************
29  * ts_packet_t
30  ******************************************************************************
31  * Describe a TS packet.
32  ******************************************************************************/
33 typedef struct ts_packet_struct
34 {
35     /* Nothing before this line, the code relies on that */
36     byte_t                  buffer[TS_PACKET_SIZE];     /* raw TS data packet */
37
38     /* Decoders information */
39     unsigned int            i_payload_start;
40                                    /* start of the PES payload in this packet */
41     unsigned int            i_payload_end;                     /* guess ? :-) */
42
43     /* Used to chain the TS packets that carry data for a same PES or PSI */
44     struct ts_packet_struct *  p_prev_ts;
45     struct ts_packet_struct *  p_next_ts;
46 } ts_packet_t;
47
48
49 /******************************************************************************
50  * pes_packet_t
51  ******************************************************************************
52  * Describes an PES packet, with its properties, and pointers to the TS packets
53  * containing it.
54  ******************************************************************************/
55 typedef struct
56 {
57     /* PES properties */
58     boolean_t               b_data_loss;   /* The previous (at least) PES packet
59             * has been lost. The decoders will have to find a way to recover. */
60     boolean_t               b_data_alignment; /* used to find the beginning of a
61                                                * video or audio unit          */
62     boolean_t               b_has_pts;        /* is the following field set ? */
63     mtime_t                 i_pts;  /* the PTS for this packet (if set above) */
64     boolean_t               b_random_access;
65              /* if TRUE, in the payload of this packet, there is the first byte 
66               * of a video sequence header, or the first byte of an audio frame.
67               */
68     u8                      i_stream_id;               /* payload type and id */
69     int                     i_pes_size;     /* size of the current PES packet */
70     int                     i_ts_packets; /* number of TS packets in this PES */
71
72     /* Demultiplexer environment */
73     boolean_t               b_discard_payload;   /* is the packet messed up ? */
74     byte_t *                p_pes_header;        /* pointer to the PES header */
75     byte_t *                p_pes_header_save;            /* temporary buffer */
76
77     /* Pointers to TS packets (TS packets are then linked by the p_prev_ts and 
78        p_next_ts fields of the ts_packet_t struct) */
79     ts_packet_t *           p_first_ts;   /* The first TS packet containing this
80                                            * PES (used by decoders). */
81     ts_packet_t *           p_last_ts; /* The last TS packet gathered at present
82                                         * (used by the demultiplexer). */
83 } pes_packet_t;
84
85
86 /******************************************************************************
87  * psi_section_t
88  ******************************************************************************
89  * Describes a PSI section. Beware, it doesn't contain pointers to the TS
90  * packets that contain it as for a PES, but the data themselves
91  ******************************************************************************/
92 typedef struct
93 {
94     byte_t        buffer[PSI_SECTION_SIZE];
95   
96     boolean_t     b_running_section;    /* Is there a section being decoded ? */
97
98     u16 i_length;
99     u16 i_current_position;
100 } psi_section_t;
101
102
103 /******************************************************************************
104  * es_descriptor_t: elementary stream descriptor
105  ******************************************************************************
106  * Describes an elementary stream, and includes fields required to handle and
107  * demultiplex this elementary stream.
108  ******************************************************************************/
109 typedef struct
110 {   
111     u16                     i_id;            /* stream ID, PID for TS streams */
112     u8                      i_type;                            /* stream type */
113
114     boolean_t               b_pcr;         /* does the stream include a PCR ? */
115     /* ?? b_pcr will be replaced by something else: since a PCR can't be shared
116      * between several ES, we will probably store the PCR fields directly here,
117      * and one of those fields will probably (again) be used as a test of the
118      * PCR presence */
119     boolean_t               b_psi;  /* does the stream have to be handled by the
120                                                                 PSI decoder ? */
121     /* Markers */
122     int                     i_continuity_counter;
123     boolean_t               b_discontinuity;
124     boolean_t               b_random;
125
126     /* PES packets */
127     pes_packet_t *          p_pes_packet;
128                                        /* current PES packet we are gathering */
129
130     /* PSI packets */
131     psi_section_t *         p_psi_section;           /* idem for a PSI stream */
132
133     /* Decoder informations */
134     void *                  p_dec;     /* p_dec is void *, since we don't know a
135                                         * priori whether it is adec_thread_t or
136                                         * vdec_thread_t. We will use explicit
137                                         * casts. */
138
139     /* ?? video stream descriptor ? */
140     /* ?? audio stream descriptor ? */
141     /* ?? hierarchy descriptor ? */
142     /* ?? target background grid descriptor ? */
143     /* ?? video window descriptor ? */
144     /* ?? ISO 639 language descriptor ? */
145
146 #ifdef STATS
147     /* Stats */
148     count_t                 c_bytes;                      /* total bytes read */
149     count_t                 c_payload_bytes;/* total of payload usefull bytes */
150     count_t                 c_packets;                  /* total packets read */
151     count_t                 c_invalid_packets;        /* invalid packets read */
152     /* ?? ... other stats */
153 #endif
154 } es_descriptor_t;
155
156 /* Special PID values - note that the PID is only on 13 bits, and that values
157  * greater than 0x1fff have no meaning in a stream */
158 #define PROGRAM_ASSOCIATION_TABLE_PID   0x0000 
159 #define CONDITIONNAL_ACCESS_TABLE_PID   0x0001                    /* not used */
160 #define EMPTY_PID                       0xffff     /* empty record in a table */
161
162 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers */
163 #define MPEG1_VIDEO_ES          0x01
164 #define MPEG2_VIDEO_ES          0x02
165 #define MPEG1_AUDIO_ES          0x03
166 #define MPEG2_AUDIO_ES          0x04
167
168
169 /******************************************************************************
170  * program_descriptor_t
171  ******************************************************************************
172  * Describes a program and list associated elementary streams. It is build by
173  * the PSI decoder upon the informations carried in program map sections 
174  ******************************************************************************/
175 typedef struct
176 {
177     /* Program characteristics */
178     u16                     i_number;                       /* program number */
179     u8                      i_version;                      /* version number */ 
180     boolean_t               b_is_ok;        /* Is the description up to date ?*/
181     u16                     i_pcr_pid;                              /* PCR ES */
182
183     int i_es_number;
184     es_descriptor_t **      ap_es;                 /* array of pointers to ES */
185
186 #ifdef DVB_EXTENSIONS
187     /* Service Descriptor (program name) */
188     u8                      i_srv_type;
189     char*                   psz_srv_name;
190 #endif
191
192     /* ?? target background grid descriptor ? */
193     /* ?? video window descriptor ? */
194     /* ?? ISO 639 language descriptor ? */
195
196 #ifdef STATS
197     /* Stats */
198     /* ?? ...stats */
199 #endif
200 } pgrm_descriptor_t;
201
202 /******************************************************************************
203  * pcr_descriptor_t
204  ******************************************************************************
205  * Contains informations used to synchronise the decoder with the server
206  ******************************************************************************/
207
208 typedef struct pcr_descriptor_struct
209 {
210     vlc_mutex_t             lock;                    /* pcr modification lock */
211
212     mtime_t                 delta_clock;
213     mtime_t                 delta_decode;
214                            /* represents decoder_time - pcr_time in usecondes */
215     mtime_t                 last_pcr;
216
217     count_t                 c_average;
218                             /* counter used to compute dynamic average values */
219     count_t                 c_pts;
220 #ifdef STATS
221     /* Stats */
222     count_t     c_average_jitter;
223     mtime_t     max_jitter;                   /* the evalueted maximum jitter */
224     mtime_t     average_jitter;               /* the evalueted average jitter */
225     count_t     c_pcr;           /* the number of PCR which have been decoded */
226 #endif
227 } pcr_descriptor_t;
228
229 /******************************************************************************
230  * stream_descriptor_t
231  ******************************************************************************
232  * Describes a transport stream and list its associated programs. Build upon
233  * the informations carried in program association sections
234  ******************************************************************************/
235 typedef struct
236 {
237     u16                     i_stream_id;                         /* stream id */
238
239     /* Program Association Table status */
240     u8                      i_PAT_version;                  /* version number */ 
241     boolean_t               b_is_PAT_complete;        /* Is the PAT complete ?*/
242     u8                      i_known_PAT_sections;
243                                       /* Number of section we received so far */
244     byte_t                  a_known_PAT_sections[32];
245                                                  /* Already received sections */
246
247     /* Program Map Table status */
248     boolean_t               b_is_PMT_complete;        /* Is the PMT complete ?*/
249     u8                      i_known_PMT_sections;
250                                       /* Number of section we received so far */
251     byte_t                  a_known_PMT_sections[32];
252                                                  /* Already received sections */
253
254     /* Service Description Table status */
255     u8                      i_SDT_version;                  /* version number */ 
256     boolean_t               b_is_SDT_complete;        /* Is the SDT complete ?*/
257     u8                      i_known_SDT_sections;
258                                       /* Number of section we received so far */
259     byte_t                  a_known_SDT_sections[32];
260                                                  /* Already received sections */
261
262     /* Programs description */
263     int i_pgrm_number;                    /* Number of program number we have */
264     pgrm_descriptor_t **    ap_programs;         /* Array of pointers to pgrm */
265
266 #ifdef STATS
267     /* Stats */
268     /* ?? ...stats */
269 #endif
270 } stream_descriptor_t;
271
272 /******************************************************************************
273  * input_netlist_t
274  ******************************************************************************/
275 typedef struct
276 {
277     vlc_mutex_t             lock;                /* netlist modification lock */
278     struct iovec            p_ts_free[INPUT_MAX_TS + INPUT_TS_READ_ONCE];
279                                            /* FIFO or LIFO of free TS packets */
280     ts_packet_t *           p_ts_packets;
281                                /* pointer to the first TS packet we allocated */
282
283     pes_packet_t *          p_pes_free[INPUT_MAX_PES + 1];
284                                           /* FIFO or LIFO of free PES packets */
285     pes_packet_t *          p_pes_packets;
286                               /* pointer to the first PES packet we allocated */
287
288     /* To use the efficiency of the scatter/gather IO operations. We implemented
289      * it in 2 ways, as we don't know yet which one is better : as a FIFO (code
290      * simplier) or as a LIFO stack (when we doesn't care of the ordering, this
291      * allow to drastically improve the cache performance) */
292 #ifdef INPUT_LIFO_TS_NETLIST
293     int                     i_ts_index;
294 #else
295     int                     i_ts_start, i_ts_end;
296 #endif
297 #ifdef INPUT_LIFO_PES_NETLIST
298     int                     i_pes_index;
299 #else
300     int                     i_pes_start, i_pes_end;
301 #endif
302 } input_netlist_t;
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 */
314 struct input_thread_struct;
315 struct input_cfg_struct;
316 typedef int (*f_open_t)( struct input_thread_struct *, struct input_cfg_struct *);
317 typedef int (*f_read_t)( struct input_thread_struct *, const struct iovec *,
318                          size_t );
319 typedef void (*f_clean_t)( struct input_thread_struct * );
320
321 typedef struct input_thread_struct
322 {
323     /* Thread properties and locks */
324     boolean_t               b_die;                              /* 'die' flag */
325     boolean_t               b_error;                              /* deadlock */
326     vlc_thread_t            thread_id;             /* id for thread functions */
327     vlc_mutex_t             programs_lock;      /* programs modification lock */
328     vlc_mutex_t             es_lock;                  /* es modification lock */
329
330     /* Input method description */
331     int                     i_method;                         /* input method */
332     int                     i_handle;               /* file/socket descriptor */
333     int                     i_vlan_id;                  /* id for vlan method */
334     f_open_t                p_open;    /* pointer to the opener of the method */
335     f_read_t                p_read;        /* pointer to the reading function */
336     f_clean_t               p_clean;    /* pointer to the 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 new decoders */
350     struct aout_thread_s *      p_aout;      /* audio output thread structure */
351
352 #ifdef STATS
353     /* Stats */
354     count_t                 c_loops;                       /* number of loops */
355     count_t                 c_bytes;                   /* total of bytes read */
356     count_t                 c_payload_bytes; /* total of payload useful bytes */
357     count_t                 c_ts_packets_read;       /* total of packets read */
358     count_t                 c_ts_packets_trashed; /* total of trashed packets */
359     /* ?? ... other stats */
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  * input_cfg_t: input thread configuration structure
373  ******************************************************************************
374  * This structure is passed as a parameter to input_CreateTtread(). It includes
375  * several fields describing potential properties of a new object. 
376  * The 'i_properties' field allow to set only a subset of the required 
377  * properties, asking the called function to use default settings for
378  * the other ones.
379  ******************************************************************************/
380 typedef struct input_cfg_struct
381 {
382     u64     i_properties;
383
384     /* Input method properties */
385     int     i_method;                                         /* input method */
386     char *  psz_filename;                                         /* filename */
387     char *  psz_hostname;                                  /* server hostname */
388     char *  psz_ip;                                              /* server IP */
389     int     i_port;                                                   /* port */
390     int     i_vlan;                                            /* vlan number */
391
392     /* ??... default settings for new decoders */
393     struct aout_thread_s *      p_aout;      /* audio output thread structure */
394
395 } input_cfg_t;
396
397 /* Properties flags */
398 #define INPUT_CFG_METHOD    (1 << 0)
399 #define INPUT_CFG_FILENAME  (1 << 4)
400 #define INPUT_CFG_HOSTNAME  (1 << 8)
401 #define INPUT_CFG_IP        (1 << 9)
402 #define INPUT_CFG_PORT      (1 << 10)
403 #define INPUT_CFG_VLAN      (1 << 11)
404
405 /*****************************************************************************
406  * Prototypes
407  *****************************************************************************/
408 input_thread_t *input_CreateThread      ( input_cfg_t *p_cfg );
409 void            input_DestroyThread     ( input_thread_t *p_input );
410
411 int             input_OpenAudioStream   ( input_thread_t *p_input, int i_pid
412                                           /* ?? , struct audio_cfg_s * p_cfg */ );
413 void            input_CloseAudioStream  ( input_thread_t *p_input, int i_pid );
414 int             input_OpenVideoStream   ( input_thread_t *p_input, 
415                                           struct vout_thread_s *p_vout, struct video_cfg_s * p_cfg );
416 void            input_CloseVideoStream  ( input_thread_t *p_input, int i_pid );
417
418 /* ?? settings functions */
419 /* ?? info functions */