]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
* demuxes: Worked around a bug in old VLC and VLS by changing TS stream types
[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.71 2002/08/30 22:22:24 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 /* 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     u16                 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
111     /* Module properties */
112     module_t *              p_module;
113     int                 ( * pf_run ) ( decoder_fifo_t * );
114 };
115
116 /*****************************************************************************
117  * bit_fifo_t : bit fifo descriptor
118  *****************************************************************************
119  * This type describes a bit fifo used to store bits while working with the
120  * input stream at the bit level.
121  *****************************************************************************/
122 typedef u32         WORD_TYPE;
123
124 typedef struct bit_fifo_t
125 {
126     /* This unsigned integer allows us to work at the bit level. This buffer
127      * can contain 32 bits, and the used space can be found on the MSb's side
128      * and the available space on the LSb's side. */
129     WORD_TYPE           buffer;
130
131     /* Number of bits available in the bit buffer */
132     int                 i_available;
133
134 } bit_fifo_t;
135
136 /*****************************************************************************
137  * bit_stream_t : bit stream descriptor
138  *****************************************************************************
139  * This type, based on a PES stream, includes all the structures needed to
140  * handle the input stream like a bit stream.
141  *****************************************************************************/
142 struct bit_stream_t
143 {
144     /*
145      * Bit structures
146      */
147     bit_fifo_t       fifo;
148
149     /*
150      * Input structures
151      */
152     /* The decoder fifo contains the data of the PES stream */
153     decoder_fifo_t * p_decoder_fifo;
154
155     /* Callback to the decoder used when changing data packets ; set
156      * to NULL if your decoder doesn't need it. */
157     void          (* pf_bitstream_callback)( bit_stream_t *, vlc_bool_t );
158     /* Optional argument to the callback */
159     void *           p_callback_arg;
160
161     /*
162      * PTS retrieval
163      */
164     mtime_t          i_pts, i_dts;
165     byte_t *         p_pts_validity;
166
167     /*
168      * Byte structures
169      */
170     /* Current data packet (in the current PES packet of the PES stream) */
171     data_packet_t *         p_data;
172     /* Pointer to the next byte that is to be read (in the current packet) */
173     byte_t *                p_byte;
174     /* Pointer to the last byte that is to be read (in the current packet */
175     byte_t *                p_end;
176     /* Temporary buffer in case we're not aligned when changing data packets */
177     WORD_TYPE               i_showbits_buffer;
178     data_packet_t           showbits_data;
179 };
180
181 /*****************************************************************************
182  * Inline functions used by the decoders to read bit_stream_t
183  *****************************************************************************/
184
185 /*
186  * DISCUSSION : How to use the bit_stream structures
187  *
188  * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
189  * minimizing the number of p_byte changes.
190  * Bits are read via GetBits() or ShowBits.
191  *
192  * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
193  * the data have to be already aligned on an 8-bit boundary, or wrong
194  * results will be returned. Use RealignBits() if unsure.
195  */
196
197 #if (WORD_TYPE == u32)
198 #   define WORD_AT      U32_AT
199 #   define WORD_SIGNED  s32
200 #elif (WORD_TYPE == u64)
201 #   define WORD_AT      U64_AT
202 #   define WORD_SIGNED  s64
203 #else
204 #   error Unsupported WORD_TYPE
205 #endif
206
207 /*****************************************************************************
208  * Prototypes from input_ext-dec.c
209  *****************************************************************************/
210 VLC_EXPORT( void, InitBitstream,  ( bit_stream_t *, decoder_fifo_t *, void ( * )( bit_stream_t *, vlc_bool_t ), void * p_callback_arg ) );
211 VLC_EXPORT( vlc_bool_t, NextDataPacket,    ( decoder_fifo_t *, data_packet_t ** ) );
212 VLC_EXPORT( void, BitstreamNextDataPacket, ( bit_stream_t * ) );
213 VLC_EXPORT( u32,  UnalignedShowBits,       ( bit_stream_t *, unsigned int ) );
214 VLC_EXPORT( void, UnalignedRemoveBits,     ( bit_stream_t * ) );
215 VLC_EXPORT( u32,  UnalignedGetBits,        ( bit_stream_t *, unsigned int ) );
216 VLC_EXPORT( void, CurrentPTS,              ( bit_stream_t *, mtime_t *, mtime_t * ) );
217 VLC_EXPORT( void, NextPTS,                 ( bit_stream_t *, mtime_t *, mtime_t * ) );
218
219 /*****************************************************************************
220  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
221  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
222  * empty bytes in the bit buffer)
223  *****************************************************************************/
224 static inline void AlignWord( bit_stream_t * p_bit_stream )
225 {
226     while( (ptrdiff_t)p_bit_stream->p_byte
227              & (sizeof(WORD_TYPE) - 1) )
228     {
229         if( p_bit_stream->p_byte < p_bit_stream->p_end )
230         {
231             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
232                 << (8 * sizeof(WORD_TYPE) - 8
233                      - p_bit_stream->fifo.i_available);
234             p_bit_stream->fifo.i_available += 8;
235         }
236         else
237         {
238             BitstreamNextDataPacket( p_bit_stream );
239             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
240                 << (8 * sizeof(WORD_TYPE) - 8
241                      - p_bit_stream->fifo.i_available);
242             p_bit_stream->fifo.i_available += 8;
243         }
244     }
245 }
246
247 /*****************************************************************************
248  * ShowBits : return i_bits bits from the bit stream
249  *****************************************************************************/
250 static inline u32 ShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
251 {
252     if( p_bit_stream->fifo.i_available >= i_bits )
253     {
254         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
255     }
256
257     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
258     {
259         return( (p_bit_stream->fifo.buffer |
260                     (WORD_AT( p_bit_stream->p_byte )
261                         >> p_bit_stream->fifo.i_available))
262                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
263     }
264
265     return( UnalignedShowBits( p_bit_stream, i_bits ) );
266 }
267
268 /*****************************************************************************
269  * ShowSignedBits : return i_bits bits from the bit stream, using signed
270  *                  arithmetic
271  *****************************************************************************/
272 static inline s32 ShowSignedBits( bit_stream_t * p_bit_stream,
273                                   unsigned int i_bits )
274 {
275     if( p_bit_stream->fifo.i_available >= i_bits )
276     {
277         return( (WORD_SIGNED)p_bit_stream->fifo.buffer
278                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
279     }
280
281     /* You can probably do something a little faster, but now I'm tired. */
282     return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
283              >> (32 - i_bits) );
284 }
285
286 /*****************************************************************************
287  * RemoveBits : removes i_bits bits from the bit buffer
288  *              XXX: do not use for 32 bits, see RemoveBits32
289  *****************************************************************************/
290 static inline void RemoveBits( bit_stream_t * p_bit_stream,
291                                unsigned int i_bits )
292 {
293     p_bit_stream->fifo.i_available -= i_bits;
294
295     if( p_bit_stream->fifo.i_available >= 0 )
296     {
297         p_bit_stream->fifo.buffer <<= i_bits;
298         return;
299     }
300
301     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
302     {
303         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
304                                         << ( -p_bit_stream->fifo.i_available );
305         p_bit_stream->p_byte =
306                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
307         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
308         return;
309     }
310
311     UnalignedRemoveBits( p_bit_stream );
312 }
313
314 /*****************************************************************************
315  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
316  *                refill it)
317  *****************************************************************************/
318 #if (WORD_TYPE == u32)
319 static inline void RemoveBits32( bit_stream_t * p_bit_stream )
320 {
321     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
322     {
323         if( p_bit_stream->fifo.i_available )
324         {
325             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
326                             << (32 - p_bit_stream->fifo.i_available);
327             p_bit_stream->p_byte =
328                        (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
329             return;
330         }
331
332         p_bit_stream->p_byte =
333                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
334         return;
335     }
336
337     p_bit_stream->fifo.i_available -= 32;
338     UnalignedRemoveBits( p_bit_stream );
339 }
340 #else
341 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
342 #endif
343
344 /*****************************************************************************
345  * GetBits : returns i_bits bits from the bit stream and removes them
346  *           XXX: do not use for 32 bits, see GetBits32
347  *****************************************************************************/
348 static inline u32 GetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
349 {
350     u32             i_result;
351
352     p_bit_stream->fifo.i_available -= i_bits;
353
354     if( p_bit_stream->fifo.i_available >= 0 )
355     {
356         i_result = p_bit_stream->fifo.buffer
357                         >> (8 * sizeof(WORD_TYPE) - i_bits);
358         p_bit_stream->fifo.buffer <<= i_bits;
359         return( i_result );
360     }
361
362     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
363     {
364         i_result = p_bit_stream->fifo.buffer
365                         >> (8 * sizeof(WORD_TYPE) - i_bits);
366         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
367         p_bit_stream->p_byte =
368                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
369         i_result |= p_bit_stream->fifo.buffer
370                         >> (8 * sizeof(WORD_TYPE)
371                                      + p_bit_stream->fifo.i_available);
372         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
373         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
374         return( i_result );
375     }
376
377     return UnalignedGetBits( p_bit_stream, i_bits );
378 }
379
380 /*****************************************************************************
381  * GetSignedBits : returns i_bits bits from the bit stream and removes them,
382  *                 using signed arithmetic
383  *                 XXX: do not use for 32 bits
384  *****************************************************************************/
385 static inline s32 GetSignedBits( bit_stream_t * p_bit_stream,
386                                  unsigned int i_bits )
387 {
388     if( p_bit_stream->fifo.i_available >= i_bits )
389     {
390         s32             i_result;
391
392         p_bit_stream->fifo.i_available -= i_bits;
393         i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
394                         >> (8 * sizeof(WORD_TYPE) - i_bits);
395         p_bit_stream->fifo.buffer <<= i_bits;
396         return( i_result );
397     }
398
399     /* You can probably do something a little faster, but now I'm tired. */
400     return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
401              >> (32 - i_bits) );
402 }
403
404 /*****************************************************************************
405  * GetBits32 : returns 32 bits from the bit stream and removes them
406  *****************************************************************************/
407 #if (WORD_TYPE == u32)
408 static inline u32 GetBits32( bit_stream_t * p_bit_stream )
409 {
410     u32             i_result;
411
412     if( p_bit_stream->fifo.i_available == 32 )
413     {
414         p_bit_stream->fifo.i_available = 0;
415         i_result = p_bit_stream->fifo.buffer;
416         p_bit_stream->fifo.buffer = 0;
417         return( i_result );
418     }
419
420     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
421     {
422         if( p_bit_stream->fifo.i_available )
423         {
424             i_result = p_bit_stream->fifo.buffer;
425             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
426             p_bit_stream->p_byte =
427                        (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
428             i_result |= p_bit_stream->fifo.buffer
429                              >> (p_bit_stream->fifo.i_available);
430             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
431             return( i_result );
432         }
433
434         i_result = WORD_AT( p_bit_stream->p_byte );
435         p_bit_stream->p_byte =
436                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
437         return( i_result );
438     }
439
440     p_bit_stream->fifo.i_available -= 32;
441     return UnalignedGetBits( p_bit_stream, 32 );
442 }
443 #else
444 #   define GetBits32( p_bit_stream )    GetBits( p_bit_stream, 32 )
445 #endif
446
447 /*****************************************************************************
448  * RealignBits : realigns the bit buffer on an 8-bit boundary
449  *****************************************************************************/
450 static inline void RealignBits( bit_stream_t * p_bit_stream )
451 {
452     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
453     p_bit_stream->fifo.i_available &= ~0x7;
454 }
455
456
457 /*****************************************************************************
458  * GetChunk : reads a large chunk of data
459  *****************************************************************************
460  * The position in the stream must be byte-aligned, if unsure call
461  * RealignBits(). p_buffer must point to a buffer at least as big as i_buf_len
462  * otherwise your code will crash.
463  *****************************************************************************/
464 static inline void GetChunk( bit_stream_t * p_bit_stream,
465                              byte_t * p_buffer, size_t i_buf_len )
466 {
467     ptrdiff_t           i_available;
468
469     /* We need to take care because i_buf_len may be < 4. */
470     while( p_bit_stream->fifo.i_available > 0 && i_buf_len )
471     {
472         *p_buffer = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - 8);
473         p_buffer++;
474         i_buf_len--;
475         p_bit_stream->fifo.buffer <<= 8;
476         p_bit_stream->fifo.i_available -= 8;
477     }
478
479     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
480             >= i_buf_len )
481     {
482         p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
483                                            p_bit_stream->p_byte, i_buf_len );
484         p_bit_stream->p_byte += i_buf_len;
485     }
486     else
487     {
488         do
489         {
490             p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
491                                            p_bit_stream->p_byte, i_available );
492             p_bit_stream->p_byte = p_bit_stream->p_end;
493             p_buffer += i_available;
494             i_buf_len -= i_available;
495             BitstreamNextDataPacket( p_bit_stream );
496             if( p_bit_stream->p_decoder_fifo->b_die )
497                 return;
498         }
499         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
500                 <= i_buf_len );
501
502         if( i_buf_len )
503         {
504             p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
505                                            p_bit_stream->p_byte, i_buf_len );
506             p_bit_stream->p_byte += i_buf_len;
507         }
508     }
509
510     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
511     {
512         AlignWord( p_bit_stream );
513     }
514 }
515
516
517 /*
518  * Communication interface between input and decoders
519  */
520
521 /*****************************************************************************
522  * Prototypes from input_dec.c
523  *****************************************************************************/
524 VLC_EXPORT( void, DecoderError, ( decoder_fifo_t * p_fifo ) );
525
526 #endif /* "input_ext-dec.h" */