]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
* Removed b_die and b_error from all decoders (obsoleted by decoder_fifo_t).
[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     boolean_t               b_error;      /* the decoder is in an error loop */
69     void *                  p_packets_mgt;   /* packets management services
70                                               * data (netlist...)            */
71     void                 (* pf_delete_pes)( void *, pes_packet_t * );
72                                      /* function to use when releasing a PES */
73 } decoder_fifo_t;
74
75 /* Macros to manage a decoder_fifo_t structure. Please remember to take
76  * data_lock before using them. */
77 #define DECODER_FIFO_ISEMPTY( fifo )  ( (fifo).i_start == (fifo).i_end )
78 #define DECODER_FIFO_ISFULL( fifo )   ( ( ((fifo).i_end + 1 - (fifo).i_start)\
79                                           & FIFO_SIZE ) == 0 )
80 #define DECODER_FIFO_START( fifo )    ( (fifo).buffer[ (fifo).i_start ] )
81 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
82                                                          & FIFO_SIZE )
83 #define DECODER_FIFO_END( fifo )      ( (fifo).buffer[ (fifo).i_end ] )
84 #define DECODER_FIFO_INCEND( fifo )   ( (fifo).i_end = ((fifo).i_end + 1) \
85                                                        & FIFO_SIZE )
86
87 /*****************************************************************************
88  * bit_fifo_t : bit fifo descriptor
89  *****************************************************************************
90  * This type describes a bit fifo used to store bits while working with the
91  * input stream at the bit level.
92  *****************************************************************************/
93 typedef u32         WORD_TYPE;        /* only u32 is supported at the moment */
94
95 typedef struct bit_fifo_s
96 {
97     /* This unsigned integer allows us to work at the bit level. This buffer
98      * can contain 32 bits, and the used space can be found on the MSb's side
99      * and the available space on the LSb's side. */
100     WORD_TYPE           buffer;
101
102     /* Number of bits available in the bit buffer */
103     int                 i_available;
104
105 } bit_fifo_t;
106
107 /*****************************************************************************
108  * bit_stream_t : bit stream descriptor
109  *****************************************************************************
110  * This type, based on a PES stream, includes all the structures needed to
111  * handle the input stream like a bit stream.
112  *****************************************************************************/
113 typedef struct bit_stream_s
114 {
115     /*
116      * Input structures
117      */
118     /* The decoder fifo contains the data of the PES stream */
119     decoder_fifo_t *        p_decoder_fifo;
120
121     /* Function to jump to the next data packet */
122     void                 (* pf_next_data_packet)( struct bit_stream_s * );
123
124     /*
125      * Byte structures
126      */
127     /* Current data packet (in the current PES packet of the PES stream) */
128     data_packet_t *         p_data;
129     /* Pointer to the next byte that is to be read (in the current TS packet) */
130     byte_t *                p_byte;
131     /* Pointer to the last byte that is to be read (in the current TS packet */
132     byte_t *                p_end;
133
134     /*
135      * Bit structures
136      */
137     bit_fifo_t              fifo;
138 } bit_stream_t;
139
140 /*****************************************************************************
141  * Inline functions used by the decoders to read bit_stream_t
142  *****************************************************************************/
143
144 /*
145  * Philosophy of the first implementation : the bit buffer is first filled by
146  * NeedBits, then the buffer can be read via p_bit_stream->fifo.buffer, and
147  * unnecessary bits are dumped with a DumpBits() call.
148  */
149
150 /*****************************************************************************
151  * GetByte : reads the next byte in the input stream
152  *****************************************************************************/
153 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
154 {
155     /* Are there some bytes left in the current data packet ? */
156     /* could change this test to have a if (! (bytes--)) instead */
157     if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
158     {
159         /* no, switch to next data packet */
160         p_bit_stream->pf_next_data_packet( p_bit_stream );
161     }
162
163     return( *(p_bit_stream->p_byte++) );
164 }
165
166 /*****************************************************************************
167  * NeedBits : reads i_bits new bits in the bit stream and stores them in the
168  *            bit buffer
169  *****************************************************************************
170  * - i_bits must be less or equal 32 !
171  * - There is something important to notice with that function : if the number
172  * of bits available in the bit buffer when calling NeedBits() is greater than
173  * 24 (i_available > 24) but less than the number of needed bits
174  * (i_available < i_bits), the byte returned by GetByte() will be shifted with
175  * a negative value and the number of bits available in the bit buffer will be
176  * set to more than 32 !
177  *****************************************************************************/
178 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
179 {
180     while ( p_bit_stream->fifo.i_available < i_bits )
181     {
182         p_bit_stream->fifo.buffer |= ((WORD_TYPE)GetByte( p_bit_stream ))
183                                      << (sizeof(WORD_TYPE) - 8
184                                             - p_bit_stream->fifo.i_available);
185         p_bit_stream->fifo.i_available += 8;
186     }
187 }
188
189 /*****************************************************************************
190  * DumpBits : removes i_bits bits from the bit buffer
191  *****************************************************************************
192  * - i_bits <= i_available
193  * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
194  *****************************************************************************/
195 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
196 {
197     p_bit_stream->fifo.buffer <<= i_bits;
198     p_bit_stream->fifo.i_available -= i_bits;
199 }
200
201
202 /*
203  * Philosophy of the second implementation : WORD_LENGTH (usually 32) bits
204  * are read at the same time, thus minimizing the number of p_byte changes.
205  * Bits are read via GetBits() or ShowBits. This is slightly faster. Be
206  * aware that if, in the forthcoming functions, i_bits > 24, the data have to
207  * be already aligned on an 8-bit boundary, or wrong results will be
208  * returned.
209  */
210
211 #if (WORD_TYPE != u32)
212 #   error Not supported word
213 #endif
214
215 /*
216  * This is stolen from the livid source who stole it from the kernel
217  * FIXME: The macro swab32 for little endian machines does
218  *        not seem to work correctly
219  */
220
221 #if defined(SYS_BEOS)
222 #   define swab32(x) B_BENDIAN_TO_HOST_INT32(x)
223 #else
224 #   if __BYTE_ORDER == __BIG_ENDIAN
225 #       define swab32(x) (x)
226 #   else
227 #       if defined (__i386__)
228 static __inline__ const u32 __i386_swab32( u32 x )
229 {
230     __asm__("bswap %0" : "=r" (x) : "0" (x));
231     return x;
232 }
233 #           define swab32(x) __i386_swab32(x)
234 #       else
235 #           define swab32(x)                                                 \
236             ( ( (u32)(((u8*)&x)[0]) << 24 ) | ( (u32)(((u8*)&x)[1]) << 16 ) |\
237               ( (u32)(((u8*)&x)[2]) << 8 )  | ( (u32)(((u8*)&x)[3])) )
238 #       endif
239 #   endif
240 #endif
241
242 /*****************************************************************************
243  * ShowBits : return i_bits bits from the bit stream
244  *****************************************************************************/
245 static __inline__ WORD_TYPE ShowWord( bit_stream_t * p_bit_stream )
246 {
247     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
248     {
249         return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
250     }
251
252     p_bit_stream->pf_next_data_packet( p_bit_stream );
253     return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
254 }
255
256 static __inline__ WORD_TYPE ShowBits( bit_stream_t * p_bit_stream, int i_bits )
257 {
258     if( p_bit_stream->fifo.i_available >= i_bits )
259     {
260         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
261     }
262
263     return( (p_bit_stream->fifo.buffer |
264             (ShowWord( p_bit_stream ) >> p_bit_stream->fifo.i_available))
265                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
266 }
267
268 /*****************************************************************************
269  * GetWord : returns the next word to be read
270  *****************************************************************************/
271 static __inline__ WORD_TYPE GetWord( bit_stream_t * p_bit_stream )
272 {
273     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
274     {
275         return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
276     }
277     else
278     {
279         p_bit_stream->pf_next_data_packet( p_bit_stream );
280         return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
281     }
282 }
283
284 /*****************************************************************************
285  * RemoveBits : removes i_bits bits from the bit buffer
286  *****************************************************************************/
287 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream, int i_bits )
288 {
289     p_bit_stream->fifo.i_available -= i_bits;
290
291     if( p_bit_stream->fifo.i_available >= 0 )
292     {
293         p_bit_stream->fifo.buffer <<= i_bits;
294         return;
295     }
296     p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
297                             << ( -p_bit_stream->fifo.i_available );
298     p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
299 }
300
301 /*****************************************************************************
302  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
303  *                refill it). This should be faster than RemoveBits, though
304  *                RemoveBits will work, too.
305  *****************************************************************************/
306 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
307 {
308     p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
309                         << (32 - p_bit_stream->fifo.i_available);
310 }
311
312 /*****************************************************************************
313  * GetBits : returns i_bits bits from the bit stream and removes them
314  *****************************************************************************/
315 static __inline__ WORD_TYPE GetBits( bit_stream_t * p_bit_stream, int i_bits )
316 {
317     u32             i_result;
318
319     p_bit_stream->fifo.i_available -= i_bits;
320     if( p_bit_stream->fifo.i_available >= 0 )
321     {
322         i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
323         p_bit_stream->fifo.buffer <<= i_bits;
324         return( i_result );
325     }
326
327     i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
328     p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
329     i_result |= p_bit_stream->fifo.buffer
330                              >> (8 * sizeof(WORD_TYPE)
331                                      + p_bit_stream->fifo.i_available);
332     p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
333     p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
334
335     return( i_result );
336 }
337
338 /*****************************************************************************
339  * GetBits32 : returns 32 bits from the bit stream and removes them
340  *****************************************************************************/
341 static __inline__ WORD_TYPE GetBits32( bit_stream_t * p_bit_stream )
342 {
343     WORD_TYPE               i_result;
344
345     i_result = p_bit_stream->fifo.buffer;
346     p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
347     i_result |= p_bit_stream->fifo.buffer
348                              >> (p_bit_stream->fifo.i_available);
349     p_bit_stream->fifo.buffer <<= (8 * sizeof(WORD_TYPE)
350                                     - p_bit_stream->fifo.i_available);
351     
352     return( i_result );
353 }
354
355 /*****************************************************************************
356  * RealignBits : realigns the bit buffer on an 8-bit boundary
357  *****************************************************************************/
358 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
359 {
360     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
361     p_bit_stream->fifo.i_available &= ~0x7;
362 }
363
364
365 /*
366  * Philosophy of the third implementation : the decoder asks for n bytes,
367  * and we will copy them in its buffer.
368  */
369
370 /*****************************************************************************
371  * GetChunk : reads a large chunk of data
372  *****************************************************************************
373  * The position in the stream must be byte-aligned, if unsure call
374  * RealignBits(). p_buffer must to a buffer at least as big as i_buf_len
375  * otherwise your code will crash.
376  *****************************************************************************/
377 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
378                                  byte_t * p_buffer, size_t i_buf_len )
379 {
380     int     i_available;
381
382     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
383             >= i_buf_len )
384     {
385         memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
386         p_bit_stream->p_byte += i_buf_len;
387     }
388     else
389     {
390         do
391         {
392             memcpy( p_buffer, p_bit_stream->p_byte, i_available );
393             p_bit_stream->p_byte = p_bit_stream->p_end;
394             p_buffer += i_available;
395             i_buf_len -= i_available;
396             p_bit_stream->pf_next_data_packet( p_bit_stream );
397         }
398         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
399                 <= i_buf_len );
400
401         if( i_buf_len )
402         {
403             memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
404             p_bit_stream->p_byte += i_buf_len;
405         }
406     }
407 }
408
409
410 /*
411  * Communication interface between input and decoders
412  */
413
414 /*****************************************************************************
415  * decoder_config_t
416  *****************************************************************************
417  * Standard pointers given to the decoders as a toolbox.
418  *****************************************************************************/
419 typedef struct decoder_config_s
420 {
421     u16                     i_stream_id;
422     u8                      i_type;         /* type of the elementary stream */
423
424     struct stream_ctrl_s *  p_stream_ctrl;
425     struct decoder_fifo_s * p_decoder_fifo;
426     void                 (* pf_init_bit_stream)( struct bit_stream_s *,
427                                                  struct decoder_fifo_s * );
428 } decoder_config_t;
429
430 /*****************************************************************************
431  * vdec_config_t
432  *****************************************************************************
433  * Pointers given to video decoders threads.
434  *****************************************************************************/
435 struct vout_thread_s;
436
437 typedef struct vdec_config_s
438 {
439     struct vout_thread_s *  p_vout;
440
441     struct picture_s *   (* pf_create_picture)( struct vout_thread_s *,
442                                                 int i_type, int i_width,
443                                                 int i_height );
444     void                 (* pf_destroy_picture)( struct vout_thread_s *,
445                                                  struct picture_s * );
446     void                 (* pf_display_picture)( struct vout_thread_s *,
447                                                  struct picture_s * );
448     void                 (* pf_date_picture)( struct vout_thread_s *,
449                                               struct picture_s *, mtime_t date );
450     void                 (* pf_link_picture)( struct vout_thread_s *,
451                                               struct picture_s *, mtime_t date );
452     void                 (* pf_unlink_picture)( struct vout_thread_s *,
453                                                 struct picture_s *, mtime_t date );
454     struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
455                                                    int i_type, int i_size );
456     void                 (* pf_destroy_subpicture)( struct vout_thread_s *,
457                                                     struct subpicture_s * );
458     void                 (* pf_display_subpicture)( struct vout_thread_s *,
459                                                     struct subpicture_s * );
460
461     decoder_config_t        decoder_config;
462 } vdec_config_t;
463
464 /*****************************************************************************
465  * adec_config_t
466  *****************************************************************************
467  * Pointers given to audio decoders threads.
468  *****************************************************************************/
469 struct aout_thread_s;
470
471 typedef struct adec_config_s
472 {
473     struct aout_thread_s *  p_aout;
474
475     struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
476                                             struct aout_fifo_s * );
477     void                 (* pf_destroy_fifo)( struct aout_thread_s *);
478
479     decoder_config_t        decoder_config;
480 } adec_config_t;
481
482
483 /*
484  * Communication interface between decoders and input
485  */
486
487 /*****************************************************************************
488  * decoder_capabilities_t
489  *****************************************************************************
490  * Structure returned by a call to GetCapabilities() of the decoder.
491  *****************************************************************************/
492 typedef struct decoder_capabilities_s
493 {
494     int                     i_dec_type;
495     u8                      i_stream_type;   /* == i_type in es_descriptor_t */
496     int                     i_weight; /* for a given stream type, the decoder
497                                        * with higher weight will be spawned  */
498
499     vlc_thread_t         (* pf_create_thread)( struct decoder_config_s * );
500 } decoder_capabilities_t;
501
502 /* Decoder types */
503 #define NONE_D              0
504 #define VIDEO_D             1
505 #define AUDIO_D             2