]> git.sesse.net Git - vlc/blob - include/input.h
. le d�codeur de sous-titres s'appelle maintenant spu_decoder
[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 #define DVD_SPU_ES             0x82           /* 0x82 might violate the norm */
152
153 /******************************************************************************
154  * program_descriptor_t
155  ******************************************************************************
156  * Describes a program and list associated elementary streams. It is build by
157  * the PSI decoder upon the informations carried in program map sections 
158  ******************************************************************************/
159 typedef struct
160 {
161     /* Program characteristics */
162     u16                     i_number;                       /* program number */
163     u8                      i_version;                      /* version number */ 
164     boolean_t               b_is_ok;        /* Is the description up to date ?*/
165     u16                     i_pcr_pid;                              /* PCR ES */
166
167     int i_es_number;
168     es_descriptor_t **      ap_es;                 /* array of pointers to ES */
169
170 #ifdef DVB_EXTENSIONS
171     /* Service Descriptor (program name) */
172     u8                      i_srv_type;
173     char*                   psz_srv_name;
174 #endif
175
176     /* ?? target background grid descriptor ? */
177     /* ?? video window descriptor ? */
178     /* ?? ISO 639 language descriptor ? */
179
180 #ifdef STATS
181     /* Stats */
182     /* ?? ...stats */
183 #endif
184 } pgrm_descriptor_t;
185
186 /******************************************************************************
187  * pcr_descriptor_t
188  ******************************************************************************
189  * Contains informations used to synchronise the decoder with the server
190  ******************************************************************************/
191
192 typedef struct pcr_descriptor_struct
193 {
194     /* system_date = PTS_date + delta_pcr + delta_absolute */
195     mtime_t                 delta_pcr;
196     mtime_t                 delta_absolute;
197
198     mtime_t                 last_pcr;
199
200     u32                     i_synchro_state;
201     count_t                 c_average_count;
202                             /* counter used to compute dynamic average values */
203 } pcr_descriptor_t;
204
205 /******************************************************************************
206  * stream_descriptor_t
207  ******************************************************************************
208  * Describes a transport stream and list its associated programs. Build upon
209  * the informations carried in program association sections
210  ******************************************************************************/
211 typedef struct
212 {
213     u16                     i_stream_id;                         /* stream id */
214
215     /* Program Association Table status */
216     u8                      i_PAT_version;                  /* version number */ 
217     boolean_t               b_is_PAT_complete;        /* Is the PAT complete ?*/
218     u8                      i_known_PAT_sections;
219                                       /* Number of section we received so far */
220     byte_t                  a_known_PAT_sections[32];
221                                                  /* Already received sections */
222
223     /* Program Map Table status */
224     boolean_t               b_is_PMT_complete;        /* Is the PMT complete ?*/
225     u8                      i_known_PMT_sections;
226                                       /* Number of section we received so far */
227     byte_t                  a_known_PMT_sections[32];
228                                                  /* Already received sections */
229
230     /* Service Description Table status */
231     u8                      i_SDT_version;                  /* version number */ 
232     boolean_t               b_is_SDT_complete;        /* Is the SDT complete ?*/
233     u8                      i_known_SDT_sections;
234                                       /* Number of section we received so far */
235     byte_t                  a_known_SDT_sections[32];
236                                                  /* Already received sections */
237
238     /* Programs description */
239     int i_pgrm_number;                    /* Number of program number we have */
240     pgrm_descriptor_t **    ap_programs;         /* Array of pointers to pgrm */
241
242 #ifdef STATS
243     /* Stats */
244     /* ?? ...stats */
245 #endif
246 } stream_descriptor_t;
247
248 /******************************************************************************
249  * input_netlist_t
250  ******************************************************************************/
251 typedef struct
252 {
253     vlc_mutex_t             lock;                /* netlist modification lock */
254     struct iovec            p_ts_free[INPUT_MAX_TS + INPUT_TS_READ_ONCE];
255                                            /* FIFO or LIFO of free TS packets */
256     ts_packet_t *           p_ts_packets;
257                                /* pointer to the first TS packet we allocated */
258
259     pes_packet_t *          p_pes_free[INPUT_MAX_PES + 1];
260                                           /* FIFO or LIFO of free PES packets */
261     pes_packet_t *          p_pes_packets;
262                               /* pointer to the first PES packet we allocated */
263
264     /* To use the efficiency of the scatter/gather IO operations. We implemented
265      * it in 2 ways, as we don't know yet which one is better : as a FIFO (code
266      * simplier) or as a LIFO stack (when we doesn't care of the ordering, this
267      * allow to drastically improve the cache performance) */
268 #ifdef INPUT_LIFO_TS_NETLIST
269     int                     i_ts_index;
270 #else
271     int                     i_ts_start, i_ts_end;
272 #endif
273 #ifdef INPUT_LIFO_PES_NETLIST
274     int                     i_pes_index;
275 #else
276     int                     i_pes_start, i_pes_end;
277 #endif
278 } input_netlist_t;
279
280
281
282 /******************************************************************************
283  * input_thread_t
284  ******************************************************************************
285  * This structure includes all the local static variables of an input thread,
286  * including the netlist and the ES descriptors
287  * Note that p_es must be defined as a static table, otherwise we would have to
288  * update all reference to it each time the table would be reallocated 
289  ******************************************************************************/
290
291 /* Function pointers used in structure */
292 typedef int  (input_open_t)     ( p_input_thread_t p_input );
293 typedef int  (input_read_t)     ( p_input_thread_t p_input, const struct iovec *p_vector,
294                                    size_t i_count );
295 typedef void (input_close_t)    ( p_input_thread_t p_input );
296
297 /* Structure */
298 typedef struct input_thread_s
299 {
300     /* Thread properties and locks */
301     boolean_t                   b_die;                          /* 'die' flag */
302     boolean_t                   b_error;                          /* deadlock */
303     vlc_thread_t                thread_id;         /* id for thread functions */
304     vlc_mutex_t                 programs_lock;  /* programs modification lock */
305     vlc_mutex_t                 es_lock;              /* es modification lock */
306     int *                       pi_status;           /* temporary status flag */
307
308     /* Input method description */
309     int                         i_method;                     /* input method */
310     int                         i_handle;           /* file/socket descriptor */    
311     char *                      psz_source;                         /* source */
312     int                         i_port;                     /* port number */
313     int                         i_vlan;                 /* id for vlan method */
314     input_open_t *              p_Open;               /* opener of the method */
315     input_read_t *              p_Read;                   /* reading function */
316     input_close_t *             p_Close;               /* destroying function */
317
318     /* General stream description */
319     stream_descriptor_t *   p_stream;                           /* PAT tables */
320     es_descriptor_t         p_es[INPUT_MAX_ES]; /* carried elementary streams */
321     pcr_descriptor_t *      p_pcr;     /* PCR struct used for synchronisation */
322
323     /* List of streams to demux */
324     es_descriptor_t *       pp_selected_es[INPUT_MAX_SELECTED_ES];
325     
326     /* Netlists */
327     input_netlist_t         netlist;                             /* see above */
328
329     /* Default settings for spawned decoders */
330     p_aout_thread_t             p_aout;      /* audio output thread structure */
331     p_vout_thread_t             p_vout;                /* video output thread */    
332
333 #ifdef STATS
334     /* Statistics */
335     count_t                     c_loops;                   /* number of loops */
336     count_t                     c_bytes;                        /* bytes read */
337     count_t                     c_payload_bytes;      /* payload useful bytes */
338     count_t                     c_packets_read;               /* packets read */
339     count_t                     c_packets_trashed;         /* trashed packets */
340 #endif
341 } input_thread_t;
342
343 /* Input methods */
344 #define INPUT_METHOD_NONE           0             /* input thread is inactive */
345 #define INPUT_METHOD_TS_FILE       10        /* TS stream is read from a file */
346 #define INPUT_METHOD_TS_UCAST      20                       /* TS UDP unicast */
347 #define INPUT_METHOD_TS_MCAST      21                     /* TS UDP multicast */
348 #define INPUT_METHOD_TS_BCAST      22                     /* TS UDP broadcast */
349 #define INPUT_METHOD_TS_VLAN_BCAST 32          /* TS UDP broadcast with VLANs */
350
351 /*****************************************************************************
352  * Prototypes
353  *****************************************************************************/
354 input_thread_t *input_CreateThread      ( int i_method, char *psz_source, int i_port, 
355                                           int i_vlan, p_vout_thread_t p_vout, 
356                                           p_aout_thread_t p_aout, int *pi_status );
357 void            input_DestroyThread     ( input_thread_t *p_input, int *pi_status );
358
359
360 int             input_OpenAudioStream   ( input_thread_t *p_input, int i_pid );
361 void            input_CloseAudioStream  ( input_thread_t *p_input, int i_pid );
362 int             input_OpenVideoStream   ( input_thread_t *p_input, int i_pid );
363 void            input_CloseVideoStream  ( input_thread_t *p_input, int i_pid );