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