]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
* Fixed a variable overflow bug in the audio output.
[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.69 2002/08/12 22:12:50 massiot 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 #ifndef _VLC_INPUT_EXT_DEC_H
26 #define _VLC_INPUT_EXT_DEC_H 1
27
28 /* ES streams types - see ISO/IEC 13818-1 table 2-29 numbers.
29  * these values are used in src/input/mpeg_system.c, and in
30  * the following plugins: mpeg_ts, mpeg_ts_dvbpsi, input_satellite. */
31 #define MPEG1_VIDEO_ES      0x01
32 #define MPEG2_VIDEO_ES      0x02
33 #define MPEG1_AUDIO_ES      0x03
34 #define MPEG2_AUDIO_ES      0x04
35 #define A52_AUDIO_ES        0x81
36 /* These ones might violate the norm : */
37 #define DVD_SPU_ES          0x82
38 #define LPCM_AUDIO_ES       0x83
39
40 /* Structures exported to the decoders */
41
42 /*****************************************************************************
43  * data_packet_t
44  *****************************************************************************
45  * Describe a data packet.
46  *****************************************************************************/
47 struct data_packet_t
48 {
49     /* Used to chain the packets that carry data for a same PES or PSI */
50     data_packet_t *  p_next;
51
52     /* start of the PS or TS packet */
53     byte_t *         p_demux_start;
54     /* start of the PES payload in this packet */
55     byte_t *         p_payload_start;
56     byte_t *         p_payload_end; /* guess ? :-) */
57     /* is the packet messed up ? */
58     vlc_bool_t       b_discard_payload;
59
60     /* pointer to the real data */
61     data_buffer_t *  p_buffer;
62 };
63
64 /*****************************************************************************
65  * pes_packet_t
66  *****************************************************************************
67  * Describes an PES packet, with its properties, and pointers to the TS packets
68  * containing it.
69  *****************************************************************************/
70 struct pes_packet_t
71 {
72     /* Chained list to the next PES packet (depending on the context) */
73     pes_packet_t *  p_next;
74
75     /* PES properties */
76     vlc_bool_t      b_data_alignment;          /* used to find the beginning of
77                                                 * a video or audio unit */
78     vlc_bool_t      b_discontinuity;          /* This packet doesn't follow the
79                                                * previous one */
80
81     mtime_t         i_pts;            /* PTS for this packet (zero if unset) */
82     mtime_t         i_dts;            /* DTS for this packet (zero if unset) */
83     int             i_rate;   /* current reading pace (see stream_control.h) */
84
85     unsigned int    i_pes_size;            /* size of the current PES packet */
86
87     /* Chained list to packets */
88     data_packet_t * p_first;              /* The first packet contained by this
89                                            * PES (used by decoders). */
90     data_packet_t * p_last;            /* The last packet contained by this
91                                         * PES (used by the buffer allocator) */
92     unsigned int    i_nb_data; /* Number of data packets in the chained list */
93 };
94
95 /*****************************************************************************
96  * decoder_fifo_t
97  *****************************************************************************
98  * This rotative FIFO contains PES packets that are to be decoded.
99  *****************************************************************************/
100 struct decoder_fifo_t
101 {
102     VLC_COMMON_MEMBERS
103
104     /* Thread structures */
105     vlc_mutex_t         data_lock;                         /* fifo data lock */
106     vlc_cond_t          data_wait;         /* fifo data conditional variable */
107
108     /* Data */
109     pes_packet_t *      p_first;
110     pes_packet_t **     pp_last;
111     int                 i_depth;       /* number of PES packets in the stack */
112
113     /* Communication interface between input and decoders */
114     input_buffers_t    *p_packets_mgt;   /* packets management services data */
115
116     /* Standard pointers given to the decoders as a toolbox. */
117     u16                 i_id;
118     vlc_fourcc_t        i_fourcc;
119     es_sys_t *          p_demux_data;
120     stream_ctrl_t *     p_stream_ctrl;
121     sout_instance_t *   p_sout;
122
123     /* Module properties */
124     module_t *              p_module;
125     int                 ( * pf_run ) ( decoder_fifo_t * );
126 };
127
128 /*****************************************************************************
129  * bit_fifo_t : bit fifo descriptor
130  *****************************************************************************
131  * This type describes a bit fifo used to store bits while working with the
132  * input stream at the bit level.
133  *****************************************************************************/
134 typedef u32         WORD_TYPE;
135
136 typedef struct bit_fifo_t
137 {
138     /* This unsigned integer allows us to work at the bit level. This buffer
139      * can contain 32 bits, and the used space can be found on the MSb's side
140      * and the available space on the LSb's side. */
141     WORD_TYPE           buffer;
142
143     /* Number of bits available in the bit buffer */
144     int                 i_available;
145
146 } bit_fifo_t;
147
148 /*****************************************************************************
149  * bit_stream_t : bit stream descriptor
150  *****************************************************************************
151  * This type, based on a PES stream, includes all the structures needed to
152  * handle the input stream like a bit stream.
153  *****************************************************************************/
154 struct bit_stream_t
155 {
156     /*
157      * Bit structures
158      */
159     bit_fifo_t       fifo;
160
161     /*
162      * Input structures
163      */
164     /* The decoder fifo contains the data of the PES stream */
165     decoder_fifo_t * p_decoder_fifo;
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)( bit_stream_t *, vlc_bool_t );
170     /* Optional argument to the callback */
171     void *           p_callback_arg;
172
173     /*
174      * PTS retrieval
175      */
176     mtime_t          i_pts, i_dts;
177     byte_t *         p_pts_validity;
178
179     /*
180      * Byte structures
181      */
182     /* Current data packet (in the current PES packet of the PES stream) */
183     data_packet_t *         p_data;
184     /* Pointer to the next byte that is to be read (in the current packet) */
185     byte_t *                p_byte;
186     /* Pointer to the last byte that is to be read (in the current packet */
187     byte_t *                p_end;
188     /* Temporary buffer in case we're not aligned when changing data packets */
189     WORD_TYPE               i_showbits_buffer;
190     data_packet_t           showbits_data;
191 };
192
193 /*****************************************************************************
194  * Inline functions used by the decoders to read bit_stream_t
195  *****************************************************************************/
196
197 /*
198  * DISCUSSION : How to use the bit_stream structures
199  *
200  * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
201  * minimizing the number of p_byte changes.
202  * Bits are read via GetBits() or ShowBits.
203  *
204  * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
205  * the data have to be already aligned on an 8-bit boundary, or wrong
206  * results will be returned. Use RealignBits() if unsure.
207  */
208
209 #if (WORD_TYPE == u32)
210 #   define WORD_AT      U32_AT
211 #   define WORD_SIGNED  s32
212 #elif (WORD_TYPE == u64)
213 #   define WORD_AT      U64_AT
214 #   define WORD_SIGNED  s64
215 #else
216 #   error Unsupported WORD_TYPE
217 #endif
218
219 /*****************************************************************************
220  * Prototypes from input_ext-dec.c
221  *****************************************************************************/
222 VLC_EXPORT( void, InitBitstream,  ( bit_stream_t *, decoder_fifo_t *, void ( * )( bit_stream_t *, vlc_bool_t ), void * p_callback_arg ) );
223 VLC_EXPORT( vlc_bool_t, NextDataPacket,    ( decoder_fifo_t *, data_packet_t ** ) );
224 VLC_EXPORT( void, BitstreamNextDataPacket, ( bit_stream_t * ) );
225 VLC_EXPORT( u32,  UnalignedShowBits,       ( bit_stream_t *, unsigned int ) );
226 VLC_EXPORT( void, UnalignedRemoveBits,     ( bit_stream_t * ) );
227 VLC_EXPORT( u32,  UnalignedGetBits,        ( bit_stream_t *, unsigned int ) );
228 VLC_EXPORT( void, CurrentPTS,              ( bit_stream_t *, mtime_t *, mtime_t * ) );
229
230 /*****************************************************************************
231  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
232  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
233  * empty bytes in the bit buffer)
234  *****************************************************************************/
235 static inline void AlignWord( bit_stream_t * p_bit_stream )
236 {
237     while( (ptrdiff_t)p_bit_stream->p_byte
238              & (sizeof(WORD_TYPE) - 1) )
239     {
240         if( p_bit_stream->p_byte < p_bit_stream->p_end )
241         {
242             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
243                 << (8 * sizeof(WORD_TYPE) - 8
244                      - p_bit_stream->fifo.i_available);
245             p_bit_stream->fifo.i_available += 8;
246         }
247         else
248         {
249             BitstreamNextDataPacket( p_bit_stream );
250             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
251                 << (8 * sizeof(WORD_TYPE) - 8
252                      - p_bit_stream->fifo.i_available);
253             p_bit_stream->fifo.i_available += 8;
254         }
255     }
256 }
257
258 /*****************************************************************************
259  * ShowBits : return i_bits bits from the bit stream
260  *****************************************************************************/
261 static inline u32 ShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
262 {
263     if( p_bit_stream->fifo.i_available >= i_bits )
264     {
265         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
266     }
267
268     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
269     {
270         return( (p_bit_stream->fifo.buffer |
271                     (WORD_AT( p_bit_stream->p_byte )
272                         >> p_bit_stream->fifo.i_available))
273                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
274     }
275
276     return( UnalignedShowBits( p_bit_stream, i_bits ) );
277 }
278
279 /*****************************************************************************
280  * ShowSignedBits : return i_bits bits from the bit stream, using signed
281  *                  arithmetic
282  *****************************************************************************/
283 static inline s32 ShowSignedBits( bit_stream_t * p_bit_stream,
284                                   unsigned int i_bits )
285 {
286     if( p_bit_stream->fifo.i_available >= i_bits )
287     {
288         return( (WORD_SIGNED)p_bit_stream->fifo.buffer
289                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
290     }
291
292     /* You can probably do something a little faster, but now I'm tired. */
293     return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
294              >> (32 - i_bits) );
295 }
296
297 /*****************************************************************************
298  * RemoveBits : removes i_bits bits from the bit buffer
299  *              XXX: do not use for 32 bits, see RemoveBits32
300  *****************************************************************************/
301 static inline void RemoveBits( bit_stream_t * p_bit_stream,
302                                unsigned int i_bits )
303 {
304     p_bit_stream->fifo.i_available -= i_bits;
305
306     if( p_bit_stream->fifo.i_available >= 0 )
307     {
308         p_bit_stream->fifo.buffer <<= i_bits;
309         return;
310     }
311
312     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
313     {
314         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
315                                         << ( -p_bit_stream->fifo.i_available );
316         p_bit_stream->p_byte =
317                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
318         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
319         return;
320     }
321
322     UnalignedRemoveBits( p_bit_stream );
323 }
324
325 /*****************************************************************************
326  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
327  *                refill it)
328  *****************************************************************************/
329 #if (WORD_TYPE == u32)
330 static inline void RemoveBits32( bit_stream_t * p_bit_stream )
331 {
332     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
333     {
334         if( p_bit_stream->fifo.i_available )
335         {
336             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
337                             << (32 - p_bit_stream->fifo.i_available);
338             p_bit_stream->p_byte =
339                        (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
340             return;
341         }
342
343         p_bit_stream->p_byte =
344                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
345         return;
346     }
347
348     p_bit_stream->fifo.i_available -= 32;
349     UnalignedRemoveBits( p_bit_stream );
350 }
351 #else
352 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
353 #endif
354
355 /*****************************************************************************
356  * GetBits : returns i_bits bits from the bit stream and removes them
357  *           XXX: do not use for 32 bits, see GetBits32
358  *****************************************************************************/
359 static inline u32 GetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
360 {
361     u32             i_result;
362
363     p_bit_stream->fifo.i_available -= i_bits;
364
365     if( p_bit_stream->fifo.i_available >= 0 )
366     {
367         i_result = p_bit_stream->fifo.buffer
368                         >> (8 * sizeof(WORD_TYPE) - i_bits);
369         p_bit_stream->fifo.buffer <<= i_bits;
370         return( i_result );
371     }
372
373     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
374     {
375         i_result = p_bit_stream->fifo.buffer
376                         >> (8 * sizeof(WORD_TYPE) - i_bits);
377         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
378         p_bit_stream->p_byte =
379                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
380         i_result |= p_bit_stream->fifo.buffer
381                         >> (8 * sizeof(WORD_TYPE)
382                                      + p_bit_stream->fifo.i_available);
383         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
384         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
385         return( i_result );
386     }
387
388     return UnalignedGetBits( p_bit_stream, i_bits );
389 }
390
391 /*****************************************************************************
392  * GetSignedBits : returns i_bits bits from the bit stream and removes them,
393  *                 using signed arithmetic
394  *                 XXX: do not use for 32 bits
395  *****************************************************************************/
396 static inline s32 GetSignedBits( bit_stream_t * p_bit_stream,
397                                  unsigned int i_bits )
398 {
399     if( p_bit_stream->fifo.i_available >= i_bits )
400     {
401         s32             i_result;
402
403         p_bit_stream->fifo.i_available -= i_bits;
404         i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
405                         >> (8 * sizeof(WORD_TYPE) - i_bits);
406         p_bit_stream->fifo.buffer <<= i_bits;
407         return( i_result );
408     }
409
410     /* You can probably do something a little faster, but now I'm tired. */
411     return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
412              >> (32 - i_bits) );
413 }
414
415 /*****************************************************************************
416  * GetBits32 : returns 32 bits from the bit stream and removes them
417  *****************************************************************************/
418 #if (WORD_TYPE == u32)
419 static inline u32 GetBits32( bit_stream_t * p_bit_stream )
420 {
421     u32             i_result;
422
423     if( p_bit_stream->fifo.i_available == 32 )
424     {
425         p_bit_stream->fifo.i_available = 0;
426         i_result = p_bit_stream->fifo.buffer;
427         p_bit_stream->fifo.buffer = 0;
428         return( i_result );
429     }
430
431     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
432     {
433         if( p_bit_stream->fifo.i_available )
434         {
435             i_result = p_bit_stream->fifo.buffer;
436             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
437             p_bit_stream->p_byte =
438                        (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
439             i_result |= p_bit_stream->fifo.buffer
440                              >> (p_bit_stream->fifo.i_available);
441             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
442             return( i_result );
443         }
444
445         i_result = WORD_AT( p_bit_stream->p_byte );
446         p_bit_stream->p_byte =
447                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
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         p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
494                                            p_bit_stream->p_byte, i_buf_len );
495         p_bit_stream->p_byte += i_buf_len;
496     }
497     else
498     {
499         do
500         {
501             p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
502                                            p_bit_stream->p_byte, i_available );
503             p_bit_stream->p_byte = p_bit_stream->p_end;
504             p_buffer += i_available;
505             i_buf_len -= i_available;
506             BitstreamNextDataPacket( p_bit_stream );
507             if( p_bit_stream->p_decoder_fifo->b_die )
508                 return;
509         }
510         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
511                 <= i_buf_len );
512
513         if( i_buf_len )
514         {
515             p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
516                                            p_bit_stream->p_byte, i_buf_len );
517             p_bit_stream->p_byte += i_buf_len;
518         }
519     }
520
521     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
522     {
523         AlignWord( p_bit_stream );
524     }
525 }
526
527
528 /*
529  * Communication interface between input and decoders
530  */
531
532 /*****************************************************************************
533  * Prototypes from input_dec.c
534  *****************************************************************************/
535 VLC_EXPORT( void, DecoderError, ( decoder_fifo_t * p_fifo ) );
536
537 #endif /* "input_ext-dec.h" */