]> git.sesse.net Git - vlc/blob - include/input.h
Changement de chaine. Delicat encore, mais il marche. Quelques corrections
[vlc] / include / input.h
1 /******************************************************************************
2  * input.h: input thread interface
3  * (c)1999 VideoLAN
4  ******************************************************************************/
5
6 /******************************************************************************
7  * Constants related to input
8  ******************************************************************************/
9 #define TS_PACKET_SIZE      188                        /* size of a TS packet */
10 #define PES_HEADER_SIZE     14      /* size of the first part of a PES header */
11 #define PSI_SECTION_SIZE    4096             /* Maximum size of a PSI section */
12
13 /******************************************************************************
14  * ts_packet_t
15  ******************************************************************************
16  * Describe a TS packet.
17  ******************************************************************************/
18 typedef struct ts_packet_s
19 {
20     /* Nothing before this line, the code relies on that */
21     byte_t                  buffer[TS_PACKET_SIZE];     /* raw TS data packet */
22
23     /* Decoders information */
24     unsigned int            i_payload_start;
25                                    /* start of the PES payload in this packet */
26     unsigned int            i_payload_end;                     /* guess ? :-) */
27
28     /* Used to chain the TS packets that carry data for a same PES or PSI */
29     struct ts_packet_s      *  p_prev_ts;
30     struct ts_packet_s      *  p_next_ts;
31 } ts_packet_t;
32
33 /******************************************************************************
34  * pes_packet_t
35  ******************************************************************************
36  * Describes an PES packet, with its properties, and pointers to the TS packets
37  * containing it.
38  ******************************************************************************/
39 typedef struct pes_packet_s
40 {
41     /* PES properties */
42     boolean_t               b_data_loss;   /* The previous (at least) PES packet
43             * has been lost. The decoders will have to find a way to recover. */
44     boolean_t               b_data_alignment; /* used to find the beginning of a
45                                                * video or audio unit          */
46     boolean_t               b_has_pts;        /* is the following field set ? */
47     mtime_t                 i_pts;  /* the PTS for this packet (if set above) */
48     boolean_t               b_random_access;
49              /* if TRUE, in the payload of this packet, there is the first byte 
50               * of a video sequence header, or the first byte of an audio frame.
51               */
52     u8                      i_stream_id;               /* payload type and id */
53     int                     i_pes_size;     /* size of the current PES packet */
54     int                     i_ts_packets; /* number of TS packets in this PES */
55
56     /* Demultiplexer environment */
57     boolean_t               b_discard_payload;   /* is the packet messed up ? */
58     byte_t *                p_pes_header;        /* pointer to the PES header */
59     byte_t *                p_pes_header_save;            /* temporary buffer */
60
61     /* Pointers to TS packets (TS packets are then linked by the p_prev_ts and 
62        p_next_ts fields of the ts_packet_t struct) */
63     ts_packet_t *           p_first_ts;   /* The first TS packet containing this
64                                            * PES (used by decoders). */
65     ts_packet_t *           p_last_ts; /* The last TS packet gathered at present
66                                         * (used by the demultiplexer). */
67 } pes_packet_t;
68
69 /******************************************************************************
70  * psi_section_t
71  ******************************************************************************
72  * Describes a PSI section. Beware, it doesn't contain pointers to the TS
73  * packets that contain it as for a PES, but the data themselves
74  ******************************************************************************/
75 typedef struct psi_section_s
76 {
77     byte_t        buffer[PSI_SECTION_SIZE];
78   
79     boolean_t     b_running_section;    /* Is there a section being decoded ? */
80
81     u16 i_length;
82     u16 i_current_position;
83 } psi_section_t;
84
85
86 /******************************************************************************
87  * es_descriptor_t: elementary stream descriptor
88  ******************************************************************************
89  * Describes an elementary stream, and includes fields required to handle and
90  * demultiplex this elementary stream.
91  ******************************************************************************/
92 typedef struct es_descriptor_t
93 {   
94     u16                     i_id;            /* stream ID, PID for TS streams */
95     u8                      i_type;                            /* stream type */
96
97     boolean_t               b_pcr;         /* does the stream include a PCR ? */
98     /* ?? b_pcr will be replaced by something else: since a PCR can't be shared
99      * between several ES, we will probably store the PCR fields directly here,
100      * and one of those fields will probably (again) be used as a test of the
101      * PCR presence */
102     boolean_t               b_psi;  /* does the stream have to be handled by the
103                                                                 PSI decoder ? */
104     /* Markers */
105     int                     i_continuity_counter;
106     boolean_t               b_discontinuity;
107     boolean_t               b_random;
108
109     /* PES packets */
110     pes_packet_t *          p_pes_packet;
111                                        /* current PES packet we are gathering */
112
113     /* PSI packets */
114     psi_section_t *         p_psi_section;           /* idem for a PSI stream */
115
116     /* Decoder informations */
117     void *                  p_dec;     /* p_dec is void *, since we don't know a
118                                         * priori whether it is adec_thread_t or
119                                         * vdec_thread_t. We will use explicit
120                                         * casts. */
121
122     /* ?? video stream descriptor ? */
123     /* ?? audio stream descriptor ? */
124     /* ?? hierarchy descriptor ? */
125     /* ?? target background grid descriptor ? */
126     /* ?? video window descriptor ? */
127     /* ?? ISO 639 language descriptor ? */
128
129 #ifdef STATS
130     /* Stats */
131     count_t                 c_bytes;                      /* total bytes read */
132     count_t                 c_payload_bytes;/* total of payload usefull bytes */
133     count_t                 c_packets;                  /* total packets read */
134     count_t                 c_invalid_packets;        /* invalid packets read */
135     /* ?? ... other stats */
136 #endif
137 } es_descriptor_t;
138
139 /* Special PID values - note that the PID is only on 13 bits, and that values
140  * greater than 0x1fff have no meaning in a stream */
141 #define PROGRAM_ASSOCIATION_TABLE_PID   0x0000 
142 #define CONDITIONNAL_ACCESS_TABLE_PID   0x0001                    /* not used */
143 #define EMPTY_PID                       0xffff     /* empty record in a table */
144
145 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers */
146 #define MPEG1_VIDEO_ES          0x01
147 #define MPEG2_VIDEO_ES          0x02
148 #define MPEG1_AUDIO_ES          0x03
149 #define MPEG2_AUDIO_ES          0x04
150 #define AC3_AUDIO_ES            0x81
151
152 /******************************************************************************
153  * program_descriptor_t
154  ******************************************************************************
155  * Describes a program and list associated elementary streams. It is build by
156  * the PSI decoder upon the informations carried in program map sections 
157  ******************************************************************************/
158 typedef struct
159 {
160     /* Program characteristics */
161     u16                     i_number;                       /* program number */
162     u8                      i_version;                      /* version number */ 
163     boolean_t               b_is_ok;        /* Is the description up to date ?*/
164     u16                     i_pcr_pid;                              /* PCR ES */
165
166     int i_es_number;
167     es_descriptor_t **      ap_es;                 /* array of pointers to ES */
168
169 #ifdef DVB_EXTENSIONS
170     /* Service Descriptor (program name) */
171     u8                      i_srv_type;
172     char*                   psz_srv_name;
173 #endif
174
175     /* ?? target background grid descriptor ? */
176     /* ?? video window descriptor ? */
177     /* ?? ISO 639 language descriptor ? */
178
179 #ifdef STATS
180     /* Stats */
181     /* ?? ...stats */
182 #endif
183 } pgrm_descriptor_t;
184
185 /******************************************************************************
186  * pcr_descriptor_t
187  ******************************************************************************
188  * Contains informations used to synchronise the decoder with the server
189  ******************************************************************************/
190
191 typedef struct pcr_descriptor_struct
192 {
193     /* system_date = PTS_date + delta_pcr + delta_absolute */
194     mtime_t                 delta_pcr;
195     mtime_t                 delta_absolute;
196
197     mtime_t                 last_pcr;
198
199     u32                     i_synchro_state;
200     count_t                 c_average_count;
201                             /* counter used to compute dynamic average values */
202 } pcr_descriptor_t;
203
204 /******************************************************************************
205  * stream_descriptor_t
206  ******************************************************************************
207  * Describes a transport stream and list its associated programs. Build upon
208  * the informations carried in program association sections
209  ******************************************************************************/
210 typedef struct
211 {
212     u16                     i_stream_id;                         /* stream id */
213
214     /* Program Association Table status */
215     u8                      i_PAT_version;                  /* version number */ 
216     boolean_t               b_is_PAT_complete;        /* Is the PAT complete ?*/
217     u8                      i_known_PAT_sections;
218                                       /* Number of section we received so far */
219     byte_t                  a_known_PAT_sections[32];
220                                                  /* Already received sections */
221
222     /* Program Map Table status */
223     boolean_t               b_is_PMT_complete;        /* Is the PMT complete ?*/
224     u8                      i_known_PMT_sections;
225                                       /* Number of section we received so far */
226     byte_t                  a_known_PMT_sections[32];
227                                                  /* Already received sections */
228
229     /* Service Description Table status */
230     u8                      i_SDT_version;                  /* version number */ 
231     boolean_t               b_is_SDT_complete;        /* Is the SDT complete ?*/
232     u8                      i_known_SDT_sections;
233                                       /* Number of section we received so far */
234     byte_t                  a_known_SDT_sections[32];
235                                                  /* Already received sections */
236
237     /* Programs description */
238     int i_pgrm_number;                    /* Number of program number we have */
239     pgrm_descriptor_t **    ap_programs;         /* Array of pointers to pgrm */
240
241 #ifdef STATS
242     /* Stats */
243     /* ?? ...stats */
244 #endif
245 } stream_descriptor_t;
246
247 /******************************************************************************
248  * input_netlist_t
249  ******************************************************************************/
250 typedef struct
251 {
252     vlc_mutex_t             lock;                /* netlist modification lock */
253     struct iovec            p_ts_free[INPUT_MAX_TS + INPUT_TS_READ_ONCE];
254                                            /* FIFO or LIFO of free TS packets */
255     ts_packet_t *           p_ts_packets;
256                                /* pointer to the first TS packet we allocated */
257
258     pes_packet_t *          p_pes_free[INPUT_MAX_PES + 1];
259                                           /* FIFO or LIFO of free PES packets */
260     pes_packet_t *          p_pes_packets;
261                               /* pointer to the first PES packet we allocated */
262
263     /* To use the efficiency of the scatter/gather IO operations. We implemented
264      * it in 2 ways, as we don't know yet which one is better : as a FIFO (code
265      * simplier) or as a LIFO stack (when we doesn't care of the ordering, this
266      * allow to drastically improve the cache performance) */
267 #ifdef INPUT_LIFO_TS_NETLIST
268     int                     i_ts_index;
269 #else
270     int                     i_ts_start, i_ts_end;
271 #endif
272 #ifdef INPUT_LIFO_PES_NETLIST
273     int                     i_pes_index;
274 #else
275     int                     i_pes_start, i_pes_end;
276 #endif
277 } input_netlist_t;
278
279
280
281 /******************************************************************************
282  * input_thread_t
283  ******************************************************************************
284  * This structure includes all the local static variables of an input thread,
285  * including the netlist and the ES descriptors
286  * Note that p_es must be defined as a static table, otherwise we would have to
287  * update all reference to it each time the table would be reallocated 
288  ******************************************************************************/
289
290 /* Function pointers used in structure */
291 typedef int  (input_open_t)     ( p_input_thread_t p_input );
292 typedef int  (input_read_t)     ( p_input_thread_t p_input, const struct iovec *p_vector,
293                                    size_t i_count );
294 typedef void (input_close_t)    ( p_input_thread_t p_input );
295
296 /* Structure */
297 typedef struct input_thread_s
298 {
299     /* Thread properties and locks */
300     boolean_t                   b_die;                          /* 'die' flag */
301     boolean_t                   b_error;                          /* deadlock */
302     vlc_thread_t                thread_id;         /* id for thread functions */
303     vlc_mutex_t                 programs_lock;  /* programs modification lock */
304     vlc_mutex_t                 es_lock;              /* es modification lock */
305     int *                       pi_status;           /* temporary status flag */
306
307     /* Input method description */
308     int                         i_method;                     /* input method */
309     int                         i_handle;           /* file/socket descriptor */    
310     char *                      psz_source;                         /* source */
311     int                         i_port;                     /* port number */
312     int                         i_vlan;                 /* id for vlan method */
313     input_open_t *              p_Open;               /* opener of the method */
314     input_read_t *              p_Read;                   /* reading function */
315     input_close_t *             p_Close;               /* destroying function */
316
317     /* General stream description */
318     stream_descriptor_t *   p_stream;                           /* PAT tables */
319     es_descriptor_t         p_es[INPUT_MAX_ES]; /* carried elementary streams */
320     pcr_descriptor_t *      p_pcr;     /* PCR struct used for synchronisation */
321
322     /* List of streams to demux */
323     es_descriptor_t *       pp_selected_es[INPUT_MAX_SELECTED_ES];
324     
325     /* Netlists */
326     input_netlist_t         netlist;                             /* see above */
327
328     /* Default settings for spawned decoders */
329     p_aout_thread_t             p_aout;      /* audio output thread structure */
330     p_vout_thread_t             p_vout;                /* video output thread */    
331
332 #ifdef STATS
333     /* Statistics */
334     count_t                     c_loops;                   /* number of loops */
335     count_t                     c_bytes;                        /* bytes read */
336     count_t                     c_payload_bytes;      /* payload useful bytes */
337     count_t                     c_packets_read;               /* packets read */
338     count_t                     c_packets_trashed;         /* trashed packets */
339 #endif
340 } input_thread_t;
341
342 /* Input methods */
343 #define INPUT_METHOD_NONE           0             /* input thread is inactive */
344 #define INPUT_METHOD_TS_FILE       10        /* TS stream is read from a file */
345 #define INPUT_METHOD_TS_UCAST      20                       /* TS UDP unicast */
346 #define INPUT_METHOD_TS_MCAST      21                     /* TS UDP multicast */
347 #define INPUT_METHOD_TS_BCAST      22                     /* TS UDP broadcast */
348 #define INPUT_METHOD_TS_VLAN_BCAST 32          /* TS UDP broadcast with VLANs */
349
350 /*****************************************************************************
351  * Prototypes
352  *****************************************************************************/
353 input_thread_t *input_CreateThread      ( int i_method, char *psz_source, int i_port, 
354                                           int i_vlan, p_vout_thread_t p_vout, 
355                                           p_aout_thread_t p_aout, int *pi_status );
356 void            input_DestroyThread     ( input_thread_t *p_input, int *pi_status );
357
358
359 int             input_OpenAudioStream   ( input_thread_t *p_input, int i_pid );
360 void            input_CloseAudioStream  ( input_thread_t *p_input, int i_pid );
361 int             input_OpenVideoStream   ( input_thread_t *p_input, int i_pid );
362 void            input_CloseVideoStream  ( input_thread_t *p_input, int i_pid );