]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
All decoders (audio, video, subtitles) are now modules.
[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.37 2001/11/13 12:09:17 henri Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Michel Kaempf <maxx@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers */
26 #define MPEG1_VIDEO_ES      0x01
27 #define MPEG2_VIDEO_ES      0x02
28 #define MPEG1_AUDIO_ES      0x03
29 #define MPEG2_AUDIO_ES      0x04
30 #define AC3_AUDIO_ES        0x81
31 /* These ones might violate the norm : */
32 #define DVD_SPU_ES          0x82
33 #define LPCM_AUDIO_ES       0x83
34 #define UNKNOWN_ES          0xFF
35
36 /* Structures exported to the decoders */
37
38 /*****************************************************************************
39  * data_packet_t
40  *****************************************************************************
41  * Describe a data packet.
42  *****************************************************************************/
43 typedef struct data_packet_s
44 {
45     /* Nothing before this line, the code relies on that */
46     byte_t *                p_buffer;                     /* raw data packet */
47     long                    l_size;                           /* buffer size */
48
49     /* Decoders information */
50     byte_t *                p_payload_start;
51                                   /* start of the PES payload in this packet */
52     byte_t *                p_payload_end;                    /* guess ? :-) */
53     boolean_t               b_discard_payload;  /* is the packet messed up ? */
54
55     int *                   pi_refcount;
56
57     /* Used to chain the TS packets that carry data for a same PES or PSI */
58     struct data_packet_s *  p_next;
59 } data_packet_t;
60
61 /*****************************************************************************
62  * pes_packet_t
63  *****************************************************************************
64  * Describes an PES packet, with its properties, and pointers to the TS packets
65  * containing it.
66  *****************************************************************************/
67 typedef struct pes_packet_s
68 {
69     /* PES properties */
70     boolean_t               b_data_alignment;  /* used to find the beginning of
71                                                 * a video or audio unit      */
72     boolean_t               b_discontinuity; /* This packet doesn't follow the
73                                               * previous one                 */
74
75     mtime_t                 i_pts;    /* PTS for this packet (zero if unset) */
76     mtime_t                 i_dts;    /* DTS for this packet (zero if unset) */
77     int                     i_rate;                /* current pace of reading
78                                                     * (see stream_control.h) */
79
80     int                     i_pes_size;    /* size of the current PES packet */
81
82     /* Pointers to packets (packets are then linked by the p_prev and
83        p_next fields of the data_packet_t struct) */
84     data_packet_t *         p_first;      /* The first packet contained by this
85                                            * PES (used by decoders). */
86 } pes_packet_t;
87
88 /*****************************************************************************
89  * decoder_fifo_t
90  *****************************************************************************
91  * This rotative FIFO contains PES packets that are to be decoded.
92  *****************************************************************************/
93 typedef struct decoder_fifo_s
94 {
95     /* Thread structures */
96     vlc_mutex_t             data_lock;                     /* fifo data lock */
97     vlc_cond_t              data_wait;     /* fifo data conditional variable */
98
99     /* Data */
100     pes_packet_t *          buffer[FIFO_SIZE + 1];
101     int                     i_start;
102     int                     i_end;
103
104     /* Communication interface between input and decoders */
105     boolean_t               b_die;          /* the decoder should return now */
106     boolean_t               b_error;      /* the decoder is in an error loop */
107     void *                  p_packets_mgt;   /* packets management services
108                                               * data (netlist...)            */
109     void                 (* pf_delete_pes)( void *, pes_packet_t * );
110                                      /* function to use when releasing a PES */
111 } decoder_fifo_t;
112
113 /* Macros to manage a decoder_fifo_t structure. Please remember to take
114  * data_lock before using them. */
115 #define DECODER_FIFO_ISEMPTY( fifo )  ( (fifo).i_start == (fifo).i_end )
116 #define DECODER_FIFO_ISFULL( fifo )   ( ( ((fifo).i_end + 1 - (fifo).i_start)\
117                                           & FIFO_SIZE ) == 0 )
118 #define DECODER_FIFO_START( fifo )    ( (fifo).buffer[ (fifo).i_start ] )
119 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
120                                                          & FIFO_SIZE )
121 #define DECODER_FIFO_END( fifo )      ( (fifo).buffer[ (fifo).i_end ] )
122 #define DECODER_FIFO_INCEND( fifo )   ( (fifo).i_end = ((fifo).i_end + 1) \
123                                                        & FIFO_SIZE )
124
125 /*****************************************************************************
126  * bit_fifo_t : bit fifo descriptor
127  *****************************************************************************
128  * This type describes a bit fifo used to store bits while working with the
129  * input stream at the bit level.
130  *****************************************************************************/
131 typedef u32         WORD_TYPE;
132
133 typedef struct bit_fifo_s
134 {
135     /* This unsigned integer allows us to work at the bit level. This buffer
136      * can contain 32 bits, and the used space can be found on the MSb's side
137      * and the available space on the LSb's side. */
138     WORD_TYPE           buffer;
139
140     /* Number of bits available in the bit buffer */
141     int                 i_available;
142
143 } bit_fifo_t;
144
145 /*****************************************************************************
146  * bit_stream_t : bit stream descriptor
147  *****************************************************************************
148  * This type, based on a PES stream, includes all the structures needed to
149  * handle the input stream like a bit stream.
150  *****************************************************************************/
151 typedef struct bit_stream_s
152 {
153     /*
154      * Bit structures
155      */
156     bit_fifo_t              fifo;
157
158     /*
159      * Input structures
160      */
161     /* The decoder fifo contains the data of the PES stream */
162     decoder_fifo_t *        p_decoder_fifo;
163
164     /* Function to jump to the next data packet */
165     void                 (* pf_next_data_packet)( struct bit_stream_s * );
166
167     /* Callback to the decoder used when changing data packets ; set
168      * to NULL if your decoder doesn't need it. */
169     void                 (* pf_bitstream_callback)( struct bit_stream_s *,
170                                                     boolean_t b_new_pes );
171     /* Optional argument to the callback */
172     void *                  p_callback_arg;
173
174     /*
175      * Byte structures
176      */
177     /* Current data packet (in the current PES packet of the PES stream) */
178     data_packet_t *         p_data;
179     /* Pointer to the next byte that is to be read (in the current packet) */
180     byte_t *                p_byte;
181     /* Pointer to the last byte that is to be read (in the current packet */
182     byte_t *                p_end;
183     /* Temporary buffer in case we're not aligned when changing data packets */
184     WORD_TYPE               i_showbits_buffer;
185     data_packet_t           showbits_data;
186 } bit_stream_t;
187
188 /*****************************************************************************
189  * Inline functions used by the decoders to read bit_stream_t
190  *****************************************************************************/
191
192 /*
193  * DISCUSSION : How to use the bit_stream structures
194  *
195  * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
196  * minimizing the number of p_byte changes.
197  * Bits are read via GetBits() or ShowBits.
198  *
199  * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
200  * the data have to be already aligned on an 8-bit boundary, or wrong
201  * results will be returned. Use RealignBits() if unsure.
202  */
203
204 #if (WORD_TYPE == u32)
205 #   define WORD_AT      U32_AT
206 #   define WORD_SIGNED  s32
207 #elif (WORD_TYPE == u64)
208 #   define WORD_AT      U64_AT
209 #   define WORD_SIGNED  s64
210 #else
211 #   error Unsupported WORD_TYPE
212 #endif
213
214 /*****************************************************************************
215  * Protoypes from input_ext-dec.c
216  *****************************************************************************/
217 #ifndef PLUGIN
218 u32  UnalignedShowBits( struct bit_stream_s *, unsigned int );
219 void UnalignedRemoveBits( struct bit_stream_s * );
220 u32  UnalignedGetBits( struct bit_stream_s *, unsigned int );
221 #endif
222
223 /*****************************************************************************
224  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
225  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
226  * empty bytes in the bit buffer)
227  *****************************************************************************/
228 static __inline__ void AlignWord( bit_stream_t * p_bit_stream )
229 {
230     while( (ptrdiff_t)p_bit_stream->p_byte
231              & (sizeof(WORD_TYPE) - 1) )
232     {
233         if( p_bit_stream->p_byte < p_bit_stream->p_end )
234         {
235             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
236                 << (8 * sizeof(WORD_TYPE) - 8
237                      - p_bit_stream->fifo.i_available);
238             p_bit_stream->fifo.i_available += 8;
239         }
240         else
241         {
242             p_bit_stream->pf_next_data_packet( p_bit_stream );
243             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
244                 << (8 * sizeof(WORD_TYPE) - 8
245                      - p_bit_stream->fifo.i_available);
246             p_bit_stream->fifo.i_available += 8;
247         }
248     }
249 }
250
251 /*****************************************************************************
252  * ShowBits : return i_bits bits from the bit stream
253  *****************************************************************************/
254 static __inline__ u32 ShowBits( bit_stream_t * p_bit_stream,
255                                 unsigned 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     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
263     {
264         return( (p_bit_stream->fifo.buffer |
265                     (WORD_AT( p_bit_stream->p_byte )
266                         >> p_bit_stream->fifo.i_available))
267                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
268     }
269
270     return( UnalignedShowBits( p_bit_stream, i_bits ) );
271 }
272
273 /*****************************************************************************
274  * ShowSignedBits : return i_bits bits from the bit stream, using signed
275  *                  arithmetic
276  *****************************************************************************/
277 static __inline__ s32 ShowSignedBits( bit_stream_t * p_bit_stream,
278                                       unsigned int i_bits )
279 {
280     if( p_bit_stream->fifo.i_available >= i_bits )
281     {
282         return( (WORD_SIGNED)p_bit_stream->fifo.buffer
283                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
284     }
285
286     /* You can probably do something a little faster, but now I'm tired. */
287     return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
288              >> (32 - i_bits) );
289 }
290
291 /*****************************************************************************
292  * RemoveBits : removes i_bits bits from the bit buffer
293  *              XXX: do not use for 32 bits, see RemoveBits32
294  *****************************************************************************/
295 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream,
296                                    unsigned int i_bits )
297 {
298     p_bit_stream->fifo.i_available -= i_bits;
299
300     if( p_bit_stream->fifo.i_available >= 0 )
301     {
302         p_bit_stream->fifo.buffer <<= i_bits;
303         return;
304     }
305
306     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
307     {
308         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
309                                         << ( -p_bit_stream->fifo.i_available );
310         ((WORD_TYPE *)p_bit_stream->p_byte)++;
311         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
312         return;
313     }
314
315     UnalignedRemoveBits( p_bit_stream );
316 }
317
318 /*****************************************************************************
319  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
320  *                refill it)
321  *****************************************************************************/
322 #if (WORD_TYPE == u32)
323 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
324 {
325     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
326     {
327         if( p_bit_stream->fifo.i_available )
328         {
329             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
330                             << (32 - p_bit_stream->fifo.i_available);
331             ((WORD_TYPE *)p_bit_stream->p_byte)++;
332             return;
333         }
334
335         ((WORD_TYPE *)p_bit_stream->p_byte)++;
336         return;
337     }
338
339     p_bit_stream->fifo.i_available -= 32;
340     UnalignedRemoveBits( p_bit_stream );
341 }
342 #else
343 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
344 #endif
345
346 /*****************************************************************************
347  * GetBits : returns i_bits bits from the bit stream and removes them
348  *           XXX: do not use for 32 bits, see GetBits32
349  *****************************************************************************/
350 static __inline__ u32 GetBits( bit_stream_t * p_bit_stream,
351                                unsigned int i_bits )
352 {
353     u32             i_result;
354
355     p_bit_stream->fifo.i_available -= i_bits;
356
357     if( p_bit_stream->fifo.i_available >= 0 )
358     {
359         i_result = p_bit_stream->fifo.buffer
360                         >> (8 * sizeof(WORD_TYPE) - i_bits);
361         p_bit_stream->fifo.buffer <<= i_bits;
362         return( i_result );
363     }
364
365     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
366     {
367         i_result = p_bit_stream->fifo.buffer
368                         >> (8 * sizeof(WORD_TYPE) - i_bits);
369         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
370         ((WORD_TYPE *)p_bit_stream->p_byte)++;
371         i_result |= p_bit_stream->fifo.buffer
372                         >> (8 * sizeof(WORD_TYPE)
373                                      + p_bit_stream->fifo.i_available);
374         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
375         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
376         return( i_result );
377     }
378
379     return UnalignedGetBits( p_bit_stream, i_bits );
380 }
381
382 /*****************************************************************************
383  * GetSignedBits : returns i_bits bits from the bit stream and removes them,
384  *                 using signed arithmetic
385  *                 XXX: do not use for 32 bits
386  *****************************************************************************/
387 static __inline__ s32 GetSignedBits( bit_stream_t * p_bit_stream,
388                                      unsigned int i_bits )
389 {
390     if( p_bit_stream->fifo.i_available >= i_bits )
391     {
392         s32             i_result;
393
394         p_bit_stream->fifo.i_available -= i_bits;
395         i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
396                         >> (8 * sizeof(WORD_TYPE) - i_bits);
397         p_bit_stream->fifo.buffer <<= i_bits;
398         return( i_result );
399     }
400
401     /* You can probably do something a little faster, but now I'm tired. */
402     return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
403              >> (32 - i_bits) );
404 }
405
406 /*****************************************************************************
407  * GetBits32 : returns 32 bits from the bit stream and removes them
408  *****************************************************************************/
409 #if (WORD_TYPE == u32)
410 static __inline__ u32 GetBits32( bit_stream_t * p_bit_stream )
411 {
412     u32             i_result;
413
414     if( p_bit_stream->fifo.i_available == 32 )
415     {
416         p_bit_stream->fifo.i_available = 0;
417         i_result = p_bit_stream->fifo.buffer;
418         p_bit_stream->fifo.buffer = 0;
419         return( i_result );
420     }
421
422     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
423     {
424         if( p_bit_stream->fifo.i_available )
425         {
426             i_result = p_bit_stream->fifo.buffer;
427             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
428             ((WORD_TYPE *)p_bit_stream->p_byte)++;
429             i_result |= p_bit_stream->fifo.buffer
430                              >> (p_bit_stream->fifo.i_available);
431             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
432             return( i_result );
433         }
434
435         i_result = WORD_AT( p_bit_stream->p_byte );
436         ((WORD_TYPE *)p_bit_stream->p_byte)++;
437         return( i_result );
438     }
439
440     p_bit_stream->fifo.i_available -= 32;
441     return UnalignedGetBits( p_bit_stream, 32 );
442 }
443 #else
444 #   define GetBits32( p_bit_stream )    GetBits( p_bit_stream, 32 )
445 #endif
446
447 /*****************************************************************************
448  * RealignBits : realigns the bit buffer on an 8-bit boundary
449  *****************************************************************************/
450 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
451 {
452     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
453     p_bit_stream->fifo.i_available &= ~0x7;
454 }
455
456
457 /*****************************************************************************
458  * GetChunk : reads a large chunk of data
459  *****************************************************************************
460  * The position in the stream must be byte-aligned, if unsure call
461  * RealignBits(). p_buffer must point to a buffer at least as big as i_buf_len
462  * otherwise your code will crash.
463  *****************************************************************************/
464 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
465                                  byte_t * p_buffer, size_t i_buf_len )
466 {
467     ptrdiff_t           i_available;
468
469     /* We need to take care because i_buf_len may be < 4. */
470     while( p_bit_stream->fifo.i_available > 0 && i_buf_len )
471     {
472         *p_buffer = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - 8);
473         p_buffer++;
474         i_buf_len--;
475         p_bit_stream->fifo.buffer <<= 8;
476         p_bit_stream->fifo.i_available -= 8;
477     }
478
479     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
480             >= i_buf_len )
481     {
482         memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
483         p_bit_stream->p_byte += i_buf_len;
484     }
485     else
486     {
487         do
488         {
489             memcpy( p_buffer, p_bit_stream->p_byte, i_available );
490             p_bit_stream->p_byte = p_bit_stream->p_end;
491             p_buffer += i_available;
492             i_buf_len -= i_available;
493             p_bit_stream->pf_next_data_packet( p_bit_stream );
494         }
495         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
496                 <= i_buf_len && !p_bit_stream->p_decoder_fifo->b_die );
497
498         if( i_buf_len )
499         {
500             memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
501             p_bit_stream->p_byte += i_buf_len;
502         }
503     }
504
505     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
506     {
507         AlignWord( p_bit_stream );
508     }
509 }
510
511
512 /*
513  * Communication interface between input and decoders
514  */
515
516 /*****************************************************************************
517  * decoder_config_t
518  *****************************************************************************
519  * Standard pointers given to the decoders as a toolbox.
520  *****************************************************************************/
521 typedef struct decoder_config_s
522 {
523     u16                     i_id;
524     u8                      i_type;         /* type of the elementary stream */
525
526     struct stream_ctrl_s *  p_stream_ctrl;
527     struct decoder_fifo_s * p_decoder_fifo;
528     void                 (* pf_init_bit_stream)( struct bit_stream_s *,
529                                                  struct decoder_fifo_s *,
530                  void (* pf_bitstream_callback)( struct bit_stream_s *,
531                                                  boolean_t ),
532                                                  void * );
533     /* Decoder thread */
534     vlc_thread_t        thread_id;
535
536     /* Plugin/Builtin properties */
537     struct module_s *   p_dec_module;
538
539 } decoder_config_t;
540
541 /*
542  * Communication interface between decoders and input
543  */
544
545 /*****************************************************************************
546  * decoder_capabilities_t
547  *****************************************************************************
548  * Structure returned by a call to GetCapabilities() of the decoder.
549  *****************************************************************************/
550 typedef struct decoder_capabilities_s
551 {
552     int                     i_dec_type;
553     u8                      i_stream_type;   /* == i_type in es_descriptor_t */
554     int                     i_weight; /* for a given stream type, the decoder
555                                        * with higher weight will be spawned  */
556
557     vlc_thread_t         (* pf_create_thread)( void * );
558 } decoder_capabilities_t;
559
560 /* Decoder types */
561 #define NONE_D              0
562 #define VIDEO_D             1
563 #define AUDIO_D             2