]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
The input-II. (more info by mail in about an hour)
[vlc] / include / input_ext-dec.h
1 /* Structures exported to the decoders */
2
3 /*****************************************************************************
4  * data_packet_t
5  *****************************************************************************
6  * Describe a data packet.
7  *****************************************************************************/
8 typedef struct data_packet_s
9 {
10     /* Nothing before this line, the code relies on that */
11     byte_t *                p_buffer;                     /* raw data packet */
12
13     /* Decoders information */
14     byte_t *                p_payload_start;
15                                   /* start of the PES payload in this packet */
16     byte_t *                p_payload_end;                    /* guess ? :-) */
17     boolean_t               b_discard_payload;  /* is the packet messed up ? */
18
19     /* Used to chain the TS packets that carry data for a same PES or PSI */
20     struct data_packet_s *  p_next;
21 } data_packet_t;
22
23 /*****************************************************************************
24  * pes_packet_t
25  *****************************************************************************
26  * Describes an PES packet, with its properties, and pointers to the TS packets
27  * containing it.
28  *****************************************************************************/
29 typedef struct pes_packet_s
30 {
31     /* PES properties */
32     boolean_t               b_messed_up;  /* At least one of the data packets
33                                            * has a questionable content      */
34     boolean_t               b_data_alignment;  /* used to find the beginning of
35                                                 * a video or audio unit      */
36     boolean_t               b_discontinuity; /* This packet doesn't follow the
37                                               * previous one                 */
38
39     boolean_t               b_has_pts;       /* is the following field set ? */
40     mtime_t                 i_pts; /* the PTS for this packet (if set above) */
41
42     int                     i_pes_size;    /* size of the current PES packet */
43
44     /* Pointers to packets (packets are then linked by the p_prev and
45        p_next fields of the data_packet_t struct) */
46     data_packet_t *         p_first;      /* The first packet containing this
47                                            * PES (used by decoders). */
48 } pes_packet_t;
49
50 /*****************************************************************************
51  * decoder_fifo_t
52  *****************************************************************************
53  * This rotative FIFO contains PES packets that are to be decoded.
54  *****************************************************************************/
55 typedef struct decoder_fifo_s
56 {
57     /* Thread structures */
58     vlc_mutex_t             data_lock;                     /* fifo data lock */
59     vlc_cond_t              data_wait;     /* fifo data conditional variable */
60
61     /* Data */
62     pes_packet_t *          buffer[FIFO_SIZE + 1];
63     int                     i_start;
64     int                     i_end;
65
66     /* Communication interface between input and decoders */
67     boolean_t               b_die;          /* the decoder should return now */
68     void *                  p_packets_mgt;   /* packets management services
69                                               * data (netlist...)            */
70     void                 (* pf_delete_pes)( void *, pes_packet_t * );
71                                      /* function to use when releasing a PES */
72 } decoder_fifo_t;
73
74 /* Macros to manage a decoder_fifo_t structure. Please remember to take
75  * data_lock before using them. */
76 #define DECODER_FIFO_ISEMPTY( fifo )  ( (fifo).i_start == (fifo).i_end )
77 #define DECODER_FIFO_ISFULL( fifo )   ( ( ((fifo).i_end + 1 - (fifo).i_start)\
78                                           & FIFO_SIZE ) == 0 )
79 #define DECODER_FIFO_START( fifo )    ( (fifo).buffer[ (fifo).i_start ] )
80 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
81                                                          & FIFO_SIZE )
82 #define DECODER_FIFO_END( fifo )      ( (fifo).buffer[ (fifo).i_end ] )
83 #define DECODER_FIFO_INCEND( fifo )   ( (fifo).i_end = ((fifo).i_end + 1) \
84                                                        & FIFO_SIZE )
85
86 /*****************************************************************************
87  * bit_fifo_t : bit fifo descriptor
88  *****************************************************************************
89  * This type describes a bit fifo used to store bits while working with the
90  * input stream at the bit level.
91  *****************************************************************************/
92 typedef u32         WORD_TYPE;        /* only u32 is supported at the moment */
93
94 typedef struct bit_fifo_s
95 {
96     /* This unsigned integer allows us to work at the bit level. This buffer
97      * can contain 32 bits, and the used space can be found on the MSb's side
98      * and the available space on the LSb's side. */
99     WORD_TYPE           buffer;
100
101     /* Number of bits available in the bit buffer */
102     int                 i_available;
103
104 } bit_fifo_t;
105
106 /*****************************************************************************
107  * bit_stream_t : bit stream descriptor
108  *****************************************************************************
109  * This type, based on a PES stream, includes all the structures needed to
110  * handle the input stream like a bit stream.
111  *****************************************************************************/
112 typedef struct bit_stream_s
113 {
114     /*
115      * Input structures
116      */
117     /* The decoder fifo contains the data of the PES stream */
118     decoder_fifo_t *        p_decoder_fifo;
119
120     /* Function to jump to the next data packet */
121     void                 (* pf_next_data_packet)( struct bit_stream_s * );
122
123     /*
124      * Byte structures
125      */
126     /* Current data packet (in the current PES packet of the PES stream) */
127     data_packet_t *         p_data;
128     /* Pointer to the next byte that is to be read (in the current TS packet) */
129     byte_t *                p_byte;
130     /* Pointer to the last byte that is to be read (in the current TS packet */
131     byte_t *                p_end;
132
133     /*
134      * Bit structures
135      */
136     bit_fifo_t              fifo;
137 } bit_stream_t;
138
139 /*****************************************************************************
140  * Inline functions used by the decoders to read bit_stream_t
141  *****************************************************************************/
142
143 /*
144  * Philosophy of the first implementation : the bit buffer is first filled by
145  * NeedBits, then the buffer can be read via p_bit_stream->fifo.buffer, and
146  * unnecessary bits are dumped with a DumpBits() call.
147  */
148
149 /*****************************************************************************
150  * GetByte : reads the next byte in the input stream
151  *****************************************************************************/
152 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
153 {
154     /* Are there some bytes left in the current data packet ? */
155     /* could change this test to have a if (! (bytes--)) instead */
156     if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
157     {
158         /* no, switch to next data packet */
159         p_bit_stream->pf_next_data_packet( p_bit_stream );
160     }
161
162     return( *(p_bit_stream->p_byte++) );
163 }
164
165 /*****************************************************************************
166  * NeedBits : reads i_bits new bits in the bit stream and stores them in the
167  *            bit buffer
168  *****************************************************************************
169  * - i_bits must be less or equal 32 !
170  * - There is something important to notice with that function : if the number
171  * of bits available in the bit buffer when calling NeedBits() is greater than
172  * 24 (i_available > 24) but less than the number of needed bits
173  * (i_available < i_bits), the byte returned by GetByte() will be shifted with
174  * a negative value and the number of bits available in the bit buffer will be
175  * set to more than 32 !
176  *****************************************************************************/
177 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
178 {
179     while ( p_bit_stream->fifo.i_available < i_bits )
180     {
181         p_bit_stream->fifo.buffer |= ((WORD_TYPE)GetByte( p_bit_stream ))
182                                      << (sizeof(WORD_TYPE) - 8
183                                             - p_bit_stream->fifo.i_available);
184         p_bit_stream->fifo.i_available += 8;
185     }
186 }
187
188 /*****************************************************************************
189  * DumpBits : removes i_bits bits from the bit buffer
190  *****************************************************************************
191  * - i_bits <= i_available
192  * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
193  *****************************************************************************/
194 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
195 {
196     p_bit_stream->fifo.buffer <<= i_bits;
197     p_bit_stream->fifo.i_available -= i_bits;
198 }
199
200
201 /*
202  * Philosophy of the second implementation : WORD_LENGTH (usually 32) bits
203  * are read at the same time, thus minimizing the number of p_byte changes.
204  * Bits are read via GetBits() or ShowBits. This is slightly faster. Be
205  * aware that if, in the forthcoming functions, i_bits > 24, the data have to
206  * be already aligned on an 8-bit boundary, or wrong results will be
207  * returned.
208  */
209
210 #if (WORD_TYPE != u32)
211 #   error Not supported word
212 #endif
213
214 /*
215  * This is stolen from the livid source who stole it from the kernel
216  * FIXME: The macro swab32 for little endian machines does
217  *        not seem to work correctly
218  */
219
220 #if defined(SYS_BEOS)
221 #   define swab32(x) B_BENDIAN_TO_HOST_INT32(x)
222 #else
223 #   if __BYTE_ORDER == __BIG_ENDIAN
224 #       define swab32(x) (x)
225 #   else
226 #       if defined (__i386__)
227 static __inline__ const u32 __i386_swab32( u32 x )
228 {
229     __asm__("bswap %0" : "=r" (x) : "0" (x));
230     return x;
231 }
232 #           define swab32(x) __i386_swab32(x)
233 #       else
234 #           define swab32(x)                                                 \
235             ( ( (u32)(((u8*)&x)[0]) << 24 ) | ( (u32)(((u8*)&x)[1]) << 16 ) |\
236               ( (u32)(((u8*)&x)[2]) << 8 )  | ( (u32)(((u8*)&x)[3])) )
237 #       endif
238 #   endif
239 #endif
240
241 /*****************************************************************************
242  * ShowBits : return i_bits bits from the bit stream
243  *****************************************************************************/
244 static __inline__ WORD_TYPE ShowWord( bit_stream_t * p_bit_stream )
245 {
246     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
247     {
248         return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
249     }
250
251     p_bit_stream->pf_next_data_packet( p_bit_stream );
252     return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
253 }
254
255 static __inline__ WORD_TYPE ShowBits( bit_stream_t * p_bit_stream, int i_bits )
256 {
257     if( p_bit_stream->fifo.i_available >= i_bits )
258     {
259         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
260     }
261
262     return( (p_bit_stream->fifo.buffer |
263             (ShowWord( p_bit_stream ) >> p_bit_stream->fifo.i_available))
264                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
265 }
266
267 /*****************************************************************************
268  * GetWord : returns the next word to be read
269  *****************************************************************************/
270 static __inline__ WORD_TYPE GetWord( bit_stream_t * p_bit_stream )
271 {
272     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
273     {
274         return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
275     }
276     else
277     {
278         p_bit_stream->pf_next_data_packet( p_bit_stream );
279         return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
280     }
281 }
282
283 /*****************************************************************************
284  * RemoveBits : removes i_bits bits from the bit buffer
285  *****************************************************************************/
286 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream, int i_bits )
287 {
288     p_bit_stream->fifo.i_available -= i_bits;
289
290     if( p_bit_stream->fifo.i_available >= 0 )
291     {
292         p_bit_stream->fifo.buffer <<= i_bits;
293         return;
294     }
295     p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
296                             << ( -p_bit_stream->fifo.i_available );
297     p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
298 }
299
300 /*****************************************************************************
301  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
302  *                refill it). This should be faster than RemoveBits, though
303  *                RemoveBits will work, too.
304  *****************************************************************************/
305 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
306 {
307     p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
308                         << (32 - p_bit_stream->fifo.i_available);
309 }
310
311 /*****************************************************************************
312  * GetBits : returns i_bits bits from the bit stream and removes them
313  *****************************************************************************/
314 static __inline__ WORD_TYPE GetBits( bit_stream_t * p_bit_stream, int i_bits )
315 {
316     u32             i_result;
317
318     p_bit_stream->fifo.i_available -= i_bits;
319     if( p_bit_stream->fifo.i_available >= 0 )
320     {
321         i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
322         p_bit_stream->fifo.buffer <<= i_bits;
323         return( i_result );
324     }
325
326     i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
327     p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
328     i_result |= p_bit_stream->fifo.buffer
329                              >> (8 * sizeof(WORD_TYPE)
330                                      + p_bit_stream->fifo.i_available);
331     p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
332     p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
333
334     return( i_result );
335 }
336
337 /*****************************************************************************
338  * GetBits32 : returns 32 bits from the bit stream and removes them
339  *****************************************************************************/
340 static __inline__ WORD_TYPE GetBits32( bit_stream_t * p_bit_stream )
341 {
342     WORD_TYPE               i_result;
343
344     i_result = p_bit_stream->fifo.buffer;
345     p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
346     i_result |= p_bit_stream->fifo.buffer
347                              >> (p_bit_stream->fifo.i_available);
348     p_bit_stream->fifo.buffer <<= (8 * sizeof(WORD_TYPE)
349                                     - p_bit_stream->fifo.i_available);
350     
351     return( i_result );
352 }
353
354 /*****************************************************************************
355  * RealignBits : realigns the bit buffer on an 8-bit boundary
356  *****************************************************************************/
357 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
358 {
359     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
360     p_bit_stream->fifo.i_available &= ~0x7;
361 }
362
363
364 /*
365  * Communication interface between input and decoders
366  */
367
368 /*****************************************************************************
369  * decoder_config_t
370  *****************************************************************************
371  * Standard pointers given to the decoders as a toolbox.
372  *****************************************************************************/
373 typedef struct decoder_config_s
374 {
375     u16                     i_stream_id;
376     u8                      i_type;         /* type of the elementary stream */
377
378     struct stream_ctrl_s *  p_stream_ctrl;
379     struct decoder_fifo_s * p_decoder_fifo;
380     void                 (* pf_init_bit_stream)( struct bit_stream_s *,
381                                                  struct decoder_fifo_s * );
382 } decoder_config_t;
383
384 /*****************************************************************************
385  * vdec_config_t
386  *****************************************************************************
387  * Pointers given to video decoders threads.
388  *****************************************************************************/
389 struct vout_thread_s;
390
391 typedef struct vdec_config_s
392 {
393     struct vout_thread_s *  p_vout;
394
395     struct picture_s *   (* pf_create_picture)( struct vout_thread_s *,
396                                                 int i_type, int i_width,
397                                                 int i_height );
398     void                 (* pf_destroy_picture)( struct vout_thread_s *,
399                                                  struct picture_s * );
400     void                 (* pf_display_picture)( struct vout_thread_s *,
401                                                  struct picture_s * );
402     void                 (* pf_date_picture)( struct vout_thread_s *,
403                                               struct picture_s *, mtime_t date );
404     void                 (* pf_link_picture)( struct vout_thread_s *,
405                                               struct picture_s *, mtime_t date );
406     void                 (* pf_unlink_picture)( struct vout_thread_s *,
407                                                 struct picture_s *, mtime_t date );
408     struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
409                                                    int i_type, int i_size );
410     void                 (* pf_destroy_subpicture)( struct vout_thread_s *,
411                                                     struct subpicture_s * );
412     void                 (* pf_display_subpicture)( struct vout_thread_s *,
413                                                     struct subpicture_s * );
414
415     decoder_config_t        decoder_config;
416 } vdec_config_t;
417
418 /*****************************************************************************
419  * adec_config_t
420  *****************************************************************************
421  * Pointers given to audio decoders threads.
422  *****************************************************************************/
423 struct aout_thread_s;
424
425 typedef struct adec_config_s
426 {
427     struct aout_thread_s *  p_aout;
428
429     struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
430                                             struct aout_fifo_s * );
431     void                 (* pf_destroy_fifo)( struct aout_thread_s *);
432
433     decoder_config_t        decoder_config;
434 } adec_config_t;
435
436
437 /*
438  * Communication interface between decoders and input
439  */
440
441 /*****************************************************************************
442  * decoder_capabilities_t
443  *****************************************************************************
444  * Structure returned by a call to GetCapabilities() of the decoder.
445  *****************************************************************************/
446 typedef struct decoder_capabilities_s
447 {
448     int                     i_dec_type;
449     u8                      i_stream_type;   /* == i_type in es_descriptor_t */
450     int                     i_weight; /* for a given stream type, the decoder
451                                        * with higher weight will be spawned  */
452
453     vlc_thread_t         (* pf_create_thread)( struct decoder_config_s * );
454 } decoder_capabilities_t;
455
456 /* Decoder types */
457 #define NONE_D              0
458 #define VIDEO_D             1
459 #define AUDIO_D             2