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