]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
* ./src/interface/main.c: we no longer segfault if argc == 0.
[vlc] / include / input_ext-dec.h
1 /*****************************************************************************
2  * input_ext-dec.h: structures exported to the VideoLAN decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: input_ext-dec.h,v 1.56 2002/04/24 00:36:24 sam 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 MSMPEG4_VIDEO_ES    0x90
35 #define MPEG4_VIDEO_ES      0x91
36 #define UNKNOWN_ES          0xFF
37
38 /* Structures exported to the decoders */
39
40 /*****************************************************************************
41  * data_packet_t
42  *****************************************************************************
43  * Describe a data packet.
44  *****************************************************************************/
45 typedef struct data_packet_s
46 {
47     /* Used to chain the packets that carry data for a same PES or PSI */
48     struct data_packet_s *  p_next;
49
50     /* start of the PS or TS packet */
51     byte_t *                p_demux_start;
52     /* start of the PES payload in this packet */
53     byte_t *                p_payload_start;
54     byte_t *                p_payload_end; /* guess ? :-) */
55     /* is the packet messed up ? */
56     boolean_t               b_discard_payload;
57
58     /* pointer to the real data */
59     struct data_buffer_s *  p_buffer;
60 } data_packet_t;
61
62 /*****************************************************************************
63  * pes_packet_t
64  *****************************************************************************
65  * Describes an PES packet, with its properties, and pointers to the TS packets
66  * containing it.
67  *****************************************************************************/
68 typedef struct pes_packet_s
69 {
70     /* Chained list to the next PES packet (depending on the context) */
71     struct pes_packet_s *   p_next;
72
73     /* PES properties */
74     boolean_t               b_data_alignment;  /* used to find the beginning of
75                                                 * a video or audio unit      */
76     boolean_t               b_discontinuity; /* This packet doesn't follow the
77                                               * previous one                 */
78
79     mtime_t                 i_pts;    /* PTS for this packet (zero if unset) */
80     mtime_t                 i_dts;    /* DTS for this packet (zero if unset) */
81     int                     i_rate;                /* current pace of reading
82                                                     * (see stream_control.h) */
83
84     unsigned int            i_pes_size;    /* size of the current PES packet */
85
86     /* Chained list to packets */
87     data_packet_t *         p_first;      /* The first packet contained by this
88                                            * PES (used by decoders). */
89     data_packet_t *         p_last;    /* The last packet contained by this
90                                           PES (used by the buffer allocator) */
91     unsigned int            i_nb_data; /* Number of data packets in the chained
92                                                                         list */
93 } pes_packet_t;
94
95 /*****************************************************************************
96  * decoder_fifo_t
97  *****************************************************************************
98  * This rotative FIFO contains PES packets that are to be decoded.
99  *****************************************************************************/
100 typedef struct decoder_fifo_s
101 {
102     /* Thread structures */
103     vlc_mutex_t             data_lock;                     /* fifo data lock */
104     vlc_cond_t              data_wait;     /* fifo data conditional variable */
105
106     /* Data */
107     pes_packet_t *          p_first;
108     pes_packet_t **         pp_last;
109     int                     i_depth;   /* number of PES packets in the stack */
110
111     /* Communication interface between input and decoders */
112     boolean_t               b_die;          /* the decoder should return now */
113     boolean_t               b_error;      /* the decoder is in an error loop */
114     struct input_buffers_s *p_packets_mgt;   /* packets management services
115                                               * data */
116 } decoder_fifo_t;
117
118 /*****************************************************************************
119  * bit_fifo_t : bit fifo descriptor
120  *****************************************************************************
121  * This type describes a bit fifo used to store bits while working with the
122  * input stream at the bit level.
123  *****************************************************************************/
124 typedef u32         WORD_TYPE;
125
126 typedef struct bit_fifo_s
127 {
128     /* This unsigned integer allows us to work at the bit level. This buffer
129      * can contain 32 bits, and the used space can be found on the MSb's side
130      * and the available space on the LSb's side. */
131     WORD_TYPE           buffer;
132
133     /* Number of bits available in the bit buffer */
134     int                 i_available;
135
136 } bit_fifo_t;
137
138 /*****************************************************************************
139  * bit_stream_t : bit stream descriptor
140  *****************************************************************************
141  * This type, based on a PES stream, includes all the structures needed to
142  * handle the input stream like a bit stream.
143  *****************************************************************************/
144 typedef struct bit_stream_s
145 {
146     /*
147      * Bit structures
148      */
149     bit_fifo_t              fifo;
150
151     /*
152      * Input structures
153      */
154     /* The decoder fifo contains the data of the PES stream */
155     decoder_fifo_t *        p_decoder_fifo;
156
157     /* Callback to the decoder used when changing data packets ; set
158      * to NULL if your decoder doesn't need it. */
159     void                 (* pf_bitstream_callback)( struct bit_stream_s *,
160                                                     boolean_t b_new_pes );
161     /* Optional argument to the callback */
162     void *                  p_callback_arg;
163
164     /*
165      * PTS retrieval
166      */
167     mtime_t                 i_pts, i_dts;
168     byte_t *                p_pts_validity;
169
170     /*
171      * Byte structures
172      */
173     /* Current data packet (in the current PES packet of the PES stream) */
174     data_packet_t *         p_data;
175     /* Pointer to the next byte that is to be read (in the current packet) */
176     byte_t *                p_byte;
177     /* Pointer to the last byte that is to be read (in the current packet */
178     byte_t *                p_end;
179     /* Temporary buffer in case we're not aligned when changing data packets */
180     WORD_TYPE               i_showbits_buffer;
181     data_packet_t           showbits_data;
182 } bit_stream_t;
183
184 /*****************************************************************************
185  * Inline functions used by the decoders to read bit_stream_t
186  *****************************************************************************/
187
188 /*
189  * DISCUSSION : How to use the bit_stream structures
190  *
191  * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
192  * minimizing the number of p_byte changes.
193  * Bits are read via GetBits() or ShowBits.
194  *
195  * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
196  * the data have to be already aligned on an 8-bit boundary, or wrong
197  * results will be returned. Use RealignBits() if unsure.
198  */
199
200 #if (WORD_TYPE == u32)
201 #   define WORD_AT      U32_AT
202 #   define WORD_SIGNED  s32
203 #elif (WORD_TYPE == u64)
204 #   define WORD_AT      U64_AT
205 #   define WORD_SIGNED  s64
206 #else
207 #   error Unsupported WORD_TYPE
208 #endif
209
210 /*****************************************************************************
211  * Prototypes from input_ext-dec.c
212  *****************************************************************************/
213 #ifndef __PLUGIN__
214 void InitBitstream  ( struct bit_stream_s *, struct decoder_fifo_s *,
215                       void (* pf_bitstream_callback)( struct bit_stream_s *,
216                                                       boolean_t ),
217                       void * p_callback_arg );
218 boolean_t NextDataPacket( struct decoder_fifo_s *, struct data_packet_s ** );
219 void BitstreamNextDataPacket( struct bit_stream_s * );
220 u32  UnalignedShowBits( struct bit_stream_s *, unsigned int );
221 void UnalignedRemoveBits( struct bit_stream_s * );
222 u32  UnalignedGetBits( struct bit_stream_s *, unsigned int );
223 void CurrentPTS( struct bit_stream_s *, mtime_t *, mtime_t * );
224 #else
225 #   define InitBitstream p_symbols->InitBitstream
226 #   define NextDataPacket p_symbols->NextDataPacket
227 #   define BitstreamNextDataPacket p_symbols->BitstreamNextDataPacket
228 #   define UnalignedShowBits p_symbols->UnalignedShowBits
229 #   define UnalignedRemoveBits p_symbols->UnalignedRemoveBits
230 #   define UnalignedGetBits p_symbols->UnalignedGetBits
231 #   define CurrentPTS p_symbols->CurrentPTS
232 #endif
233
234 /*****************************************************************************
235  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
236  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
237  * empty bytes in the bit buffer)
238  *****************************************************************************/
239 static __inline__ void AlignWord( bit_stream_t * p_bit_stream )
240 {
241     while( (ptrdiff_t)p_bit_stream->p_byte
242              & (sizeof(WORD_TYPE) - 1) )
243     {
244         if( p_bit_stream->p_byte < p_bit_stream->p_end )
245         {
246             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
247                 << (8 * sizeof(WORD_TYPE) - 8
248                      - p_bit_stream->fifo.i_available);
249             p_bit_stream->fifo.i_available += 8;
250         }
251         else
252         {
253             BitstreamNextDataPacket( p_bit_stream );
254             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
255                 << (8 * sizeof(WORD_TYPE) - 8
256                      - p_bit_stream->fifo.i_available);
257             p_bit_stream->fifo.i_available += 8;
258         }
259     }
260 }
261
262 /*****************************************************************************
263  * ShowBits : return i_bits bits from the bit stream
264  *****************************************************************************/
265 static __inline__ u32 ShowBits( bit_stream_t * p_bit_stream,
266                                 unsigned int i_bits )
267 {
268     if( p_bit_stream->fifo.i_available >= i_bits )
269     {
270         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
271     }
272
273     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
274     {
275         return( (p_bit_stream->fifo.buffer |
276                     (WORD_AT( p_bit_stream->p_byte )
277                         >> p_bit_stream->fifo.i_available))
278                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
279     }
280
281     return( UnalignedShowBits( p_bit_stream, i_bits ) );
282 }
283
284 /*****************************************************************************
285  * ShowSignedBits : return i_bits bits from the bit stream, using signed
286  *                  arithmetic
287  *****************************************************************************/
288 static __inline__ s32 ShowSignedBits( bit_stream_t * p_bit_stream,
289                                       unsigned int i_bits )
290 {
291     if( p_bit_stream->fifo.i_available >= i_bits )
292     {
293         return( (WORD_SIGNED)p_bit_stream->fifo.buffer
294                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
295     }
296
297     /* You can probably do something a little faster, but now I'm tired. */
298     return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
299              >> (32 - i_bits) );
300 }
301
302 /*****************************************************************************
303  * RemoveBits : removes i_bits bits from the bit buffer
304  *              XXX: do not use for 32 bits, see RemoveBits32
305  *****************************************************************************/
306 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream,
307                                    unsigned int i_bits )
308 {
309     p_bit_stream->fifo.i_available -= i_bits;
310
311     if( p_bit_stream->fifo.i_available >= 0 )
312     {
313         p_bit_stream->fifo.buffer <<= i_bits;
314         return;
315     }
316
317     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
318     {
319         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
320                                         << ( -p_bit_stream->fifo.i_available );
321         ((WORD_TYPE *)p_bit_stream->p_byte)++;
322         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
323         return;
324     }
325
326     UnalignedRemoveBits( p_bit_stream );
327 }
328
329 /*****************************************************************************
330  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
331  *                refill it)
332  *****************************************************************************/
333 #if (WORD_TYPE == u32)
334 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
335 {
336     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
337     {
338         if( p_bit_stream->fifo.i_available )
339         {
340             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
341                             << (32 - p_bit_stream->fifo.i_available);
342             ((WORD_TYPE *)p_bit_stream->p_byte)++;
343             return;
344         }
345
346         ((WORD_TYPE *)p_bit_stream->p_byte)++;
347         return;
348     }
349
350     p_bit_stream->fifo.i_available -= 32;
351     UnalignedRemoveBits( p_bit_stream );
352 }
353 #else
354 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
355 #endif
356
357 /*****************************************************************************
358  * GetBits : returns i_bits bits from the bit stream and removes them
359  *           XXX: do not use for 32 bits, see GetBits32
360  *****************************************************************************/
361 static __inline__ u32 GetBits( bit_stream_t * p_bit_stream,
362                                unsigned int i_bits )
363 {
364     u32             i_result;
365
366     p_bit_stream->fifo.i_available -= i_bits;
367
368     if( p_bit_stream->fifo.i_available >= 0 )
369     {
370         i_result = p_bit_stream->fifo.buffer
371                         >> (8 * sizeof(WORD_TYPE) - i_bits);
372         p_bit_stream->fifo.buffer <<= i_bits;
373         return( i_result );
374     }
375
376     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
377     {
378         i_result = p_bit_stream->fifo.buffer
379                         >> (8 * sizeof(WORD_TYPE) - i_bits);
380         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
381         ((WORD_TYPE *)p_bit_stream->p_byte)++;
382         i_result |= p_bit_stream->fifo.buffer
383                         >> (8 * sizeof(WORD_TYPE)
384                                      + p_bit_stream->fifo.i_available);
385         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
386         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
387         return( i_result );
388     }
389
390     return UnalignedGetBits( p_bit_stream, i_bits );
391 }
392
393 /*****************************************************************************
394  * GetSignedBits : returns i_bits bits from the bit stream and removes them,
395  *                 using signed arithmetic
396  *                 XXX: do not use for 32 bits
397  *****************************************************************************/
398 static __inline__ s32 GetSignedBits( bit_stream_t * p_bit_stream,
399                                      unsigned int i_bits )
400 {
401     if( p_bit_stream->fifo.i_available >= i_bits )
402     {
403         s32             i_result;
404
405         p_bit_stream->fifo.i_available -= i_bits;
406         i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
407                         >> (8 * sizeof(WORD_TYPE) - i_bits);
408         p_bit_stream->fifo.buffer <<= i_bits;
409         return( i_result );
410     }
411
412     /* You can probably do something a little faster, but now I'm tired. */
413     return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
414              >> (32 - i_bits) );
415 }
416
417 /*****************************************************************************
418  * GetBits32 : returns 32 bits from the bit stream and removes them
419  *****************************************************************************/
420 #if (WORD_TYPE == u32)
421 static __inline__ u32 GetBits32( bit_stream_t * p_bit_stream )
422 {
423     u32             i_result;
424
425     if( p_bit_stream->fifo.i_available == 32 )
426     {
427         p_bit_stream->fifo.i_available = 0;
428         i_result = p_bit_stream->fifo.buffer;
429         p_bit_stream->fifo.buffer = 0;
430         return( i_result );
431     }
432
433     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
434     {
435         if( p_bit_stream->fifo.i_available )
436         {
437             i_result = p_bit_stream->fifo.buffer;
438             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
439             ((WORD_TYPE *)p_bit_stream->p_byte)++;
440             i_result |= p_bit_stream->fifo.buffer
441                              >> (p_bit_stream->fifo.i_available);
442             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
443             return( i_result );
444         }
445
446         i_result = WORD_AT( p_bit_stream->p_byte );
447         ((WORD_TYPE *)p_bit_stream->p_byte)++;
448         return( i_result );
449     }
450
451     p_bit_stream->fifo.i_available -= 32;
452     return UnalignedGetBits( p_bit_stream, 32 );
453 }
454 #else
455 #   define GetBits32( p_bit_stream )    GetBits( p_bit_stream, 32 )
456 #endif
457
458 /*****************************************************************************
459  * RealignBits : realigns the bit buffer on an 8-bit boundary
460  *****************************************************************************/
461 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
462 {
463     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
464     p_bit_stream->fifo.i_available &= ~0x7;
465 }
466
467
468 /*****************************************************************************
469  * GetChunk : reads a large chunk of data
470  *****************************************************************************
471  * The position in the stream must be byte-aligned, if unsure call
472  * RealignBits(). p_buffer must point to a buffer at least as big as i_buf_len
473  * otherwise your code will crash.
474  *****************************************************************************/
475 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
476                                  byte_t * p_buffer, size_t i_buf_len )
477 {
478     ptrdiff_t           i_available;
479
480     /* We need to take care because i_buf_len may be < 4. */
481     while( p_bit_stream->fifo.i_available > 0 && i_buf_len )
482     {
483         *p_buffer = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - 8);
484         p_buffer++;
485         i_buf_len--;
486         p_bit_stream->fifo.buffer <<= 8;
487         p_bit_stream->fifo.i_available -= 8;
488     }
489
490     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
491             >= i_buf_len )
492     {
493         FAST_MEMCPY( p_buffer, p_bit_stream->p_byte, i_buf_len );
494         p_bit_stream->p_byte += i_buf_len;
495     }
496     else
497     {
498         do
499         {
500             FAST_MEMCPY( p_buffer, p_bit_stream->p_byte, i_available );
501             p_bit_stream->p_byte = p_bit_stream->p_end;
502             p_buffer += i_available;
503             i_buf_len -= i_available;
504             BitstreamNextDataPacket( p_bit_stream );
505         }
506         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
507                 <= i_buf_len && !p_bit_stream->p_decoder_fifo->b_die );
508
509         if( i_buf_len )
510         {
511             FAST_MEMCPY( p_buffer, p_bit_stream->p_byte, i_buf_len );
512             p_bit_stream->p_byte += i_buf_len;
513         }
514     }
515
516     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
517     {
518         AlignWord( p_bit_stream );
519     }
520 }
521
522
523 /*
524  * Communication interface between input and decoders
525  */
526
527 /*****************************************************************************
528  * decoder_config_t
529  *****************************************************************************
530  * Standard pointers given to the decoders as a toolbox.
531  *****************************************************************************/
532 typedef struct decoder_config_s
533 {
534     u16                     i_id;
535     u8                      i_type;         /* type of the elementary stream */
536
537     void *                  p_demux_data;
538     struct stream_ctrl_s *  p_stream_ctrl;
539     struct decoder_fifo_s * p_decoder_fifo;
540 } decoder_config_t;
541
542 /*****************************************************************************
543  * Prototypes from input_dec.c
544  *****************************************************************************/
545 #ifndef __PLUGIN__
546 void DecoderError      ( struct decoder_fifo_s * p_fifo );
547 #else
548 #   define DecoderError p_symbols->DecoderError
549 #endif
550