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