]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
GetChunk() : reads n bytes from the elementary stream and places them
[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  * Philosophy of the third implementation : the decoder asks for n bytes,
366  * and we will copy them in its buffer.
367  */
368
369 /*****************************************************************************
370  * GetChunk : reads a large chunk of data
371  *****************************************************************************
372  * The position in the stream must be byte-aligned, if unsure call
373  * RealignBits(). p_buffer must to a buffer at least as big as i_buf_len
374  * otherwise your code will crash.
375  *****************************************************************************/
376 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
377                                  byte_t * p_buffer, size_t i_buf_len )
378 {
379     int     i_available;
380
381     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
382             >= i_buf_len )
383     {
384         memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
385         p_bit_stream->p_byte += i_buf_len;
386     }
387     else
388     {
389         do
390         {
391             memcpy( p_buffer, p_bit_stream->p_byte, i_available );
392             p_bit_stream->p_byte = p_bit_stream->p_end;
393             p_buffer += i_available;
394             i_buf_len -= i_available;
395             p_bit_stream->pf_next_data_packet( p_bit_stream );
396         }
397         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
398                 <= i_buf_len );
399
400         if( i_buf_len )
401         {
402             memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
403             p_bit_stream->p_byte += i_buf_len;
404         }
405     }
406 }
407
408
409 /*
410  * Communication interface between input and decoders
411  */
412
413 /*****************************************************************************
414  * decoder_config_t
415  *****************************************************************************
416  * Standard pointers given to the decoders as a toolbox.
417  *****************************************************************************/
418 typedef struct decoder_config_s
419 {
420     u16                     i_stream_id;
421     u8                      i_type;         /* type of the elementary stream */
422
423     struct stream_ctrl_s *  p_stream_ctrl;
424     struct decoder_fifo_s * p_decoder_fifo;
425     void                 (* pf_init_bit_stream)( struct bit_stream_s *,
426                                                  struct decoder_fifo_s * );
427 } decoder_config_t;
428
429 /*****************************************************************************
430  * vdec_config_t
431  *****************************************************************************
432  * Pointers given to video decoders threads.
433  *****************************************************************************/
434 struct vout_thread_s;
435
436 typedef struct vdec_config_s
437 {
438     struct vout_thread_s *  p_vout;
439
440     struct picture_s *   (* pf_create_picture)( struct vout_thread_s *,
441                                                 int i_type, int i_width,
442                                                 int i_height );
443     void                 (* pf_destroy_picture)( struct vout_thread_s *,
444                                                  struct picture_s * );
445     void                 (* pf_display_picture)( struct vout_thread_s *,
446                                                  struct picture_s * );
447     void                 (* pf_date_picture)( struct vout_thread_s *,
448                                               struct picture_s *, mtime_t date );
449     void                 (* pf_link_picture)( struct vout_thread_s *,
450                                               struct picture_s *, mtime_t date );
451     void                 (* pf_unlink_picture)( struct vout_thread_s *,
452                                                 struct picture_s *, mtime_t date );
453     struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
454                                                    int i_type, int i_size );
455     void                 (* pf_destroy_subpicture)( struct vout_thread_s *,
456                                                     struct subpicture_s * );
457     void                 (* pf_display_subpicture)( struct vout_thread_s *,
458                                                     struct subpicture_s * );
459
460     decoder_config_t        decoder_config;
461 } vdec_config_t;
462
463 /*****************************************************************************
464  * adec_config_t
465  *****************************************************************************
466  * Pointers given to audio decoders threads.
467  *****************************************************************************/
468 struct aout_thread_s;
469
470 typedef struct adec_config_s
471 {
472     struct aout_thread_s *  p_aout;
473
474     struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
475                                             struct aout_fifo_s * );
476     void                 (* pf_destroy_fifo)( struct aout_thread_s *);
477
478     decoder_config_t        decoder_config;
479 } adec_config_t;
480
481
482 /*
483  * Communication interface between decoders and input
484  */
485
486 /*****************************************************************************
487  * decoder_capabilities_t
488  *****************************************************************************
489  * Structure returned by a call to GetCapabilities() of the decoder.
490  *****************************************************************************/
491 typedef struct decoder_capabilities_s
492 {
493     int                     i_dec_type;
494     u8                      i_stream_type;   /* == i_type in es_descriptor_t */
495     int                     i_weight; /* for a given stream type, the decoder
496                                        * with higher weight will be spawned  */
497
498     vlc_thread_t         (* pf_create_thread)( struct decoder_config_s * );
499 } decoder_capabilities_t;
500
501 /* Decoder types */
502 #define NONE_D              0
503 #define VIDEO_D             1
504 #define AUDIO_D             2