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