]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
* ./src/misc/modules_plugin.h: if symbol foo isn't found in the
[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.52 2002/01/21 23:57:46 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 /* 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 #define DATA_PACKET                                                         \
44     /* start of the PS or TS packet */                                      \
45     byte_t *                p_demux_start;                                  \
46     /* start of the PES payload in this packet */                           \
47     byte_t *                p_payload_start;                                \
48     byte_t *                p_payload_end; /* guess ? :-) */                \
49     /* is the packet messed up ? */                                         \
50     boolean_t               b_discard_payload;
51
52 typedef struct data_packet_s
53 {
54     /* Used to chain the packets that carry data for a same PES or PSI */
55     struct data_packet_s *  p_next;
56
57     DATA_PACKET
58
59     /* Please note that at least one buffer allocator (in particular, the
60      * Next Generation Buffer Allocator) extends this structure with
61      * private data after DATA_PACKET. */
62 } data_packet_t;
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 typedef struct pes_packet_s
71 {
72     /* Chained list to the next PES packet (depending on the context) */
73     struct pes_packet_s *   p_next;
74
75     /* PES properties */
76     boolean_t               b_data_alignment;  /* used to find the beginning of
77                                                 * a video or audio unit      */
78     boolean_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 pace of reading
84                                                     * (see stream_control.h) */
85
86     unsigned int            i_pes_size;    /* size of the current PES packet */
87
88     /* Chained list to packets */
89     data_packet_t *         p_first;      /* The first packet contained by this
90                                            * PES (used by decoders). */
91     data_packet_t *         p_last;    /* The last packet contained by this
92                                           PES (used by the buffer allocator) */
93     unsigned int            i_nb_data; /* Number of data packets in the chained
94                                                                         list */
95 } pes_packet_t;
96
97 /*****************************************************************************
98  * decoder_fifo_t
99  *****************************************************************************
100  * This rotative FIFO contains PES packets that are to be decoded.
101  *****************************************************************************/
102 typedef struct decoder_fifo_s
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     boolean_t               b_die;          /* the decoder should return now */
115     boolean_t               b_error;      /* the decoder is in an error loop */
116     void *                  p_packets_mgt;   /* packets management services
117                                               * data (netlist...)            */
118     void                 (* pf_delete_pes)( void *, pes_packet_t * );
119                                      /* function to use when releasing a PES */
120 } decoder_fifo_t;
121
122 /*****************************************************************************
123  * bit_fifo_t : bit fifo descriptor
124  *****************************************************************************
125  * This type describes a bit fifo used to store bits while working with the
126  * input stream at the bit level.
127  *****************************************************************************/
128 typedef u32         WORD_TYPE;
129
130 typedef struct bit_fifo_s
131 {
132     /* This unsigned integer allows us to work at the bit level. This buffer
133      * can contain 32 bits, and the used space can be found on the MSb's side
134      * and the available space on the LSb's side. */
135     WORD_TYPE           buffer;
136
137     /* Number of bits available in the bit buffer */
138     int                 i_available;
139
140 } bit_fifo_t;
141
142 /*****************************************************************************
143  * bit_stream_t : bit stream descriptor
144  *****************************************************************************
145  * This type, based on a PES stream, includes all the structures needed to
146  * handle the input stream like a bit stream.
147  *****************************************************************************/
148 typedef struct bit_stream_s
149 {
150     /*
151      * Bit structures
152      */
153     bit_fifo_t              fifo;
154
155     /*
156      * Input structures
157      */
158     /* The decoder fifo contains the data of the PES stream */
159     decoder_fifo_t *        p_decoder_fifo;
160
161     /* Callback to the decoder used when changing data packets ; set
162      * to NULL if your decoder doesn't need it. */
163     void                 (* pf_bitstream_callback)( struct bit_stream_s *,
164                                                     boolean_t b_new_pes );
165     /* Optional argument to the callback */
166     void *                  p_callback_arg;
167
168     /*
169      * PTS retrieval
170      */
171     mtime_t                 i_pts, i_dts;
172     byte_t *                p_pts_validity;
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  * Prototypes from input_ext-dec.c
216  *****************************************************************************/
217 #ifndef PLUGIN
218 void InitBitstream  ( struct bit_stream_s *, struct decoder_fifo_s *,
219                       void (* pf_bitstream_callback)( struct bit_stream_s *,
220                                                       boolean_t ),
221                       void * p_callback_arg );
222 boolean_t NextDataPacket( struct decoder_fifo_s *, struct data_packet_s ** );
223 void BitstreamNextDataPacket( struct bit_stream_s * );
224 u32  UnalignedShowBits( struct bit_stream_s *, unsigned int );
225 void UnalignedRemoveBits( struct bit_stream_s * );
226 u32  UnalignedGetBits( struct bit_stream_s *, unsigned int );
227 void CurrentPTS( struct bit_stream_s *, mtime_t *, mtime_t * );
228 #else
229 #   define InitBitstream p_symbols->InitBitstream
230 #   define NextDataPacket p_symbols->NextDataPacket
231 #   define BitstreamNextDataPacket p_symbols->BitstreamNextDataPacket
232 #   define UnalignedShowBits p_symbols->UnalignedShowBits
233 #   define UnalignedRemoveBits p_symbols->UnalignedRemoveBits
234 #   define UnalignedGetBits p_symbols->UnalignedGetBits
235 #   define CurrentPTS p_symbols->CurrentPTS
236 #endif
237
238 /*****************************************************************************
239  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
240  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
241  * empty bytes in the bit buffer)
242  *****************************************************************************/
243 static __inline__ void AlignWord( bit_stream_t * p_bit_stream )
244 {
245     while( (ptrdiff_t)p_bit_stream->p_byte
246              & (sizeof(WORD_TYPE) - 1) )
247     {
248         if( p_bit_stream->p_byte < p_bit_stream->p_end )
249         {
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         else
256         {
257             BitstreamNextDataPacket( p_bit_stream );
258             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
259                 << (8 * sizeof(WORD_TYPE) - 8
260                      - p_bit_stream->fifo.i_available);
261             p_bit_stream->fifo.i_available += 8;
262         }
263     }
264 }
265
266 /*****************************************************************************
267  * ShowBits : return i_bits bits from the bit stream
268  *****************************************************************************/
269 static __inline__ u32 ShowBits( bit_stream_t * p_bit_stream,
270                                 unsigned int i_bits )
271 {
272     if( p_bit_stream->fifo.i_available >= i_bits )
273     {
274         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
275     }
276
277     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
278     {
279         return( (p_bit_stream->fifo.buffer |
280                     (WORD_AT( p_bit_stream->p_byte )
281                         >> p_bit_stream->fifo.i_available))
282                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
283     }
284
285     return( UnalignedShowBits( p_bit_stream, i_bits ) );
286 }
287
288 /*****************************************************************************
289  * ShowSignedBits : return i_bits bits from the bit stream, using signed
290  *                  arithmetic
291  *****************************************************************************/
292 static __inline__ s32 ShowSignedBits( bit_stream_t * p_bit_stream,
293                                       unsigned int i_bits )
294 {
295     if( p_bit_stream->fifo.i_available >= i_bits )
296     {
297         return( (WORD_SIGNED)p_bit_stream->fifo.buffer
298                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
299     }
300
301     /* You can probably do something a little faster, but now I'm tired. */
302     return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
303              >> (32 - i_bits) );
304 }
305
306 /*****************************************************************************
307  * RemoveBits : removes i_bits bits from the bit buffer
308  *              XXX: do not use for 32 bits, see RemoveBits32
309  *****************************************************************************/
310 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream,
311                                    unsigned int i_bits )
312 {
313     p_bit_stream->fifo.i_available -= i_bits;
314
315     if( p_bit_stream->fifo.i_available >= 0 )
316     {
317         p_bit_stream->fifo.buffer <<= i_bits;
318         return;
319     }
320
321     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
322     {
323         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
324                                         << ( -p_bit_stream->fifo.i_available );
325         ((WORD_TYPE *)p_bit_stream->p_byte)++;
326         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
327         return;
328     }
329
330     UnalignedRemoveBits( p_bit_stream );
331 }
332
333 /*****************************************************************************
334  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
335  *                refill it)
336  *****************************************************************************/
337 #if (WORD_TYPE == u32)
338 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
339 {
340     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
341     {
342         if( p_bit_stream->fifo.i_available )
343         {
344             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
345                             << (32 - p_bit_stream->fifo.i_available);
346             ((WORD_TYPE *)p_bit_stream->p_byte)++;
347             return;
348         }
349
350         ((WORD_TYPE *)p_bit_stream->p_byte)++;
351         return;
352     }
353
354     p_bit_stream->fifo.i_available -= 32;
355     UnalignedRemoveBits( p_bit_stream );
356 }
357 #else
358 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
359 #endif
360
361 /*****************************************************************************
362  * GetBits : returns i_bits bits from the bit stream and removes them
363  *           XXX: do not use for 32 bits, see GetBits32
364  *****************************************************************************/
365 static __inline__ u32 GetBits( bit_stream_t * p_bit_stream,
366                                unsigned int i_bits )
367 {
368     u32             i_result;
369
370     p_bit_stream->fifo.i_available -= i_bits;
371
372     if( p_bit_stream->fifo.i_available >= 0 )
373     {
374         i_result = p_bit_stream->fifo.buffer
375                         >> (8 * sizeof(WORD_TYPE) - i_bits);
376         p_bit_stream->fifo.buffer <<= i_bits;
377         return( i_result );
378     }
379
380     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
381     {
382         i_result = p_bit_stream->fifo.buffer
383                         >> (8 * sizeof(WORD_TYPE) - i_bits);
384         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
385         ((WORD_TYPE *)p_bit_stream->p_byte)++;
386         i_result |= p_bit_stream->fifo.buffer
387                         >> (8 * sizeof(WORD_TYPE)
388                                      + p_bit_stream->fifo.i_available);
389         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
390         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
391         return( i_result );
392     }
393
394     return UnalignedGetBits( p_bit_stream, i_bits );
395 }
396
397 /*****************************************************************************
398  * GetSignedBits : returns i_bits bits from the bit stream and removes them,
399  *                 using signed arithmetic
400  *                 XXX: do not use for 32 bits
401  *****************************************************************************/
402 static __inline__ s32 GetSignedBits( bit_stream_t * p_bit_stream,
403                                      unsigned int i_bits )
404 {
405     if( p_bit_stream->fifo.i_available >= i_bits )
406     {
407         s32             i_result;
408
409         p_bit_stream->fifo.i_available -= i_bits;
410         i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
411                         >> (8 * sizeof(WORD_TYPE) - i_bits);
412         p_bit_stream->fifo.buffer <<= i_bits;
413         return( i_result );
414     }
415
416     /* You can probably do something a little faster, but now I'm tired. */
417     return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
418              >> (32 - i_bits) );
419 }
420
421 /*****************************************************************************
422  * GetBits32 : returns 32 bits from the bit stream and removes them
423  *****************************************************************************/
424 #if (WORD_TYPE == u32)
425 static __inline__ u32 GetBits32( bit_stream_t * p_bit_stream )
426 {
427     u32             i_result;
428
429     if( p_bit_stream->fifo.i_available == 32 )
430     {
431         p_bit_stream->fifo.i_available = 0;
432         i_result = p_bit_stream->fifo.buffer;
433         p_bit_stream->fifo.buffer = 0;
434         return( i_result );
435     }
436
437     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
438     {
439         if( p_bit_stream->fifo.i_available )
440         {
441             i_result = p_bit_stream->fifo.buffer;
442             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
443             ((WORD_TYPE *)p_bit_stream->p_byte)++;
444             i_result |= p_bit_stream->fifo.buffer
445                              >> (p_bit_stream->fifo.i_available);
446             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
447             return( i_result );
448         }
449
450         i_result = WORD_AT( p_bit_stream->p_byte );
451         ((WORD_TYPE *)p_bit_stream->p_byte)++;
452         return( i_result );
453     }
454
455     p_bit_stream->fifo.i_available -= 32;
456     return UnalignedGetBits( p_bit_stream, 32 );
457 }
458 #else
459 #   define GetBits32( p_bit_stream )    GetBits( p_bit_stream, 32 )
460 #endif
461
462 /*****************************************************************************
463  * RealignBits : realigns the bit buffer on an 8-bit boundary
464  *****************************************************************************/
465 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
466 {
467     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
468     p_bit_stream->fifo.i_available &= ~0x7;
469 }
470
471
472 /*****************************************************************************
473  * GetChunk : reads a large chunk of data
474  *****************************************************************************
475  * The position in the stream must be byte-aligned, if unsure call
476  * RealignBits(). p_buffer must point to a buffer at least as big as i_buf_len
477  * otherwise your code will crash.
478  *****************************************************************************/
479 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
480                                  byte_t * p_buffer, size_t i_buf_len )
481 {
482     ptrdiff_t           i_available;
483
484     /* We need to take care because i_buf_len may be < 4. */
485     while( p_bit_stream->fifo.i_available > 0 && i_buf_len )
486     {
487         *p_buffer = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - 8);
488         p_buffer++;
489         i_buf_len--;
490         p_bit_stream->fifo.buffer <<= 8;
491         p_bit_stream->fifo.i_available -= 8;
492     }
493
494     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
495             >= i_buf_len )
496     {
497         FAST_MEMCPY( p_buffer, p_bit_stream->p_byte, i_buf_len );
498         p_bit_stream->p_byte += i_buf_len;
499     }
500     else
501     {
502         do
503         {
504             FAST_MEMCPY( p_buffer, p_bit_stream->p_byte, i_available );
505             p_bit_stream->p_byte = p_bit_stream->p_end;
506             p_buffer += i_available;
507             i_buf_len -= i_available;
508             BitstreamNextDataPacket( p_bit_stream );
509         }
510         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
511                 <= i_buf_len && !p_bit_stream->p_decoder_fifo->b_die );
512
513         if( i_buf_len )
514         {
515             FAST_MEMCPY( p_buffer, p_bit_stream->p_byte, i_buf_len );
516             p_bit_stream->p_byte += i_buf_len;
517         }
518     }
519
520     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
521     {
522         AlignWord( p_bit_stream );
523     }
524 }
525
526
527 /*
528  * Communication interface between input and decoders
529  */
530
531 /*****************************************************************************
532  * decoder_config_t
533  *****************************************************************************
534  * Standard pointers given to the decoders as a toolbox.
535  *****************************************************************************/
536 typedef struct decoder_config_s
537 {
538     u16                     i_id;
539     u8                      i_type;         /* type of the elementary stream */
540
541     struct stream_ctrl_s *  p_stream_ctrl;
542     struct decoder_fifo_s * p_decoder_fifo;
543 } decoder_config_t;
544
545 /*****************************************************************************
546  * Prototypes from input_dec.c
547  *****************************************************************************/
548 #ifndef PLUGIN
549 void DecoderError      ( struct decoder_fifo_s * p_fifo );
550 #else
551 #   define DecoderError p_symbols->DecoderError
552 #endif
553