]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
* Implemented basic stream navigation function, and bound Jump forward
[vlc] / include / input_ext-dec.h
1 /*****************************************************************************
2  * input_ext-dec.h: structures exported to the VideoLAN decoders
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: input_ext-dec.h,v 1.21 2001/02/08 13:52:34 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 /* Structures exported to the decoders */
26
27 /*****************************************************************************
28  * data_packet_t
29  *****************************************************************************
30  * Describe a data packet.
31  *****************************************************************************/
32 typedef struct data_packet_s
33 {
34     /* Nothing before this line, the code relies on that */
35     byte_t *                p_buffer;                     /* raw data packet */
36
37     /* Decoders information */
38     byte_t *                p_payload_start;
39                                   /* start of the PES payload in this packet */
40     byte_t *                p_payload_end;                    /* guess ? :-) */
41     boolean_t               b_discard_payload;  /* is the packet messed up ? */
42
43     /* Used to chain the TS packets that carry data for a same PES or PSI */
44     struct data_packet_s *  p_next;
45 } data_packet_t;
46
47 /*****************************************************************************
48  * pes_packet_t
49  *****************************************************************************
50  * Describes an PES packet, with its properties, and pointers to the TS packets
51  * containing it.
52  *****************************************************************************/
53 typedef struct pes_packet_s
54 {
55     /* PES properties */
56     boolean_t               b_messed_up;  /* At least one of the data packets
57                                            * has a questionable content      */
58     boolean_t               b_data_alignment;  /* used to find the beginning of
59                                                 * a video or audio unit      */
60     boolean_t               b_discontinuity; /* This packet doesn't follow the
61                                               * previous one                 */
62
63     mtime_t                 i_pts;    /* PTS for this packet (zero if unset) */
64     mtime_t                 i_dts;    /* DTS for this packet (zero if unset) */
65     int                     i_rate;                /* current pace of reading
66                                                     * (see stream_control.h) */
67
68     int                     i_pes_size;    /* size of the current PES packet */
69
70     /* Pointers to packets (packets are then linked by the p_prev and
71        p_next fields of the data_packet_t struct) */
72     data_packet_t *         p_first;      /* The first packet contained by this
73                                            * PES (used by decoders). */
74 } pes_packet_t;
75
76 /*****************************************************************************
77  * decoder_fifo_t
78  *****************************************************************************
79  * This rotative FIFO contains PES packets that are to be decoded.
80  *****************************************************************************/
81 typedef struct decoder_fifo_s
82 {
83     /* Thread structures */
84     vlc_mutex_t             data_lock;                     /* fifo data lock */
85     vlc_cond_t              data_wait;     /* fifo data conditional variable */
86
87     /* Data */
88     pes_packet_t *          buffer[FIFO_SIZE + 1];
89     int                     i_start;
90     int                     i_end;
91
92     /* Communication interface between input and decoders */
93     boolean_t               b_die;          /* the decoder should return now */
94     boolean_t               b_error;      /* the decoder is in an error loop */
95     void *                  p_packets_mgt;   /* packets management services
96                                               * data (netlist...)            */
97     void                 (* pf_delete_pes)( void *, pes_packet_t * );
98                                      /* function to use when releasing a PES */
99 } decoder_fifo_t;
100
101 /* Macros to manage a decoder_fifo_t structure. Please remember to take
102  * data_lock before using them. */
103 #define DECODER_FIFO_ISEMPTY( fifo )  ( (fifo).i_start == (fifo).i_end )
104 #define DECODER_FIFO_ISFULL( fifo )   ( ( ((fifo).i_end + 1 - (fifo).i_start)\
105                                           & FIFO_SIZE ) == 0 )
106 #define DECODER_FIFO_START( fifo )    ( (fifo).buffer[ (fifo).i_start ] )
107 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
108                                                          & FIFO_SIZE )
109 #define DECODER_FIFO_END( fifo )      ( (fifo).buffer[ (fifo).i_end ] )
110 #define DECODER_FIFO_INCEND( fifo )   ( (fifo).i_end = ((fifo).i_end + 1) \
111                                                        & FIFO_SIZE )
112
113 /*****************************************************************************
114  * bit_fifo_t : bit fifo descriptor
115  *****************************************************************************
116  * This type describes a bit fifo used to store bits while working with the
117  * input stream at the bit level.
118  *****************************************************************************/
119 typedef u32         WORD_TYPE;
120
121 typedef struct bit_fifo_s
122 {
123     /* This unsigned integer allows us to work at the bit level. This buffer
124      * can contain 32 bits, and the used space can be found on the MSb's side
125      * and the available space on the LSb's side. */
126     WORD_TYPE           buffer;
127
128     /* Number of bits available in the bit buffer */
129     int                 i_available;
130
131 } bit_fifo_t;
132
133 /*****************************************************************************
134  * bit_stream_t : bit stream descriptor
135  *****************************************************************************
136  * This type, based on a PES stream, includes all the structures needed to
137  * handle the input stream like a bit stream.
138  *****************************************************************************/
139 typedef struct bit_stream_s
140 {
141     /*
142      * Bit structures
143      */
144     bit_fifo_t              fifo;
145
146     /*
147      * Input structures
148      */
149     /* The decoder fifo contains the data of the PES stream */
150     decoder_fifo_t *        p_decoder_fifo;
151
152     /* Function to jump to the next data packet */
153     void                 (* pf_next_data_packet)( struct bit_stream_s * );
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)( struct bit_stream_s *,
158                                                     boolean_t b_new_pes );
159     /* Optional argument to the callback */
160     void *                  p_callback_arg;
161
162     /*
163      * Byte structures
164      */
165     /* Current data packet (in the current PES packet of the PES stream) */
166     data_packet_t *         p_data;
167     /* Pointer to the next byte that is to be read (in the current TS packet) */
168     byte_t *                p_byte;
169     /* Pointer to the last byte that is to be read (in the current TS packet */
170     byte_t *                p_end;
171 } bit_stream_t;
172
173 /*****************************************************************************
174  * Inline functions used by the decoders to read bit_stream_t
175  *****************************************************************************/
176
177 /*
178  * DISCUSSION : How to use the bit_stream structures
179  *
180  * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
181  * minimizing the number of p_byte changes.
182  * Bits are read via GetBits() or ShowBits.
183  *
184  * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
185  * the data have to be already aligned on an 8-bit boundary, or wrong
186  * results will be returned. Use RealignBits() if unsure.
187  */
188
189 #if (WORD_TYPE == u32)
190 #   define WORD_AT      U32_AT
191 #elif (WORD_TYPE == u64)
192 #   define WORD_AT      U64_AT
193 #else
194 #   error Unsupported WORD_TYPE
195 #endif
196
197 /*****************************************************************************
198  * Protoypes from input_ext-dec.c
199  *****************************************************************************/
200 u32  UnalignedShowBits( struct bit_stream_s *, unsigned int );
201 void UnalignedRemoveBits( struct bit_stream_s * );
202 u32  UnalignedGetBits( struct bit_stream_s *, unsigned int );
203
204 /*****************************************************************************
205  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
206  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
207  * empty bytes in the bit buffer)
208  *****************************************************************************/
209 static __inline__ void AlignWord( bit_stream_t * p_bit_stream )
210 {
211     while( (p_bit_stream->p_byte - p_bit_stream->p_data->p_buffer)
212              & (sizeof(WORD_TYPE) - 1) )
213     {
214         if( p_bit_stream->p_byte < p_bit_stream->p_end )
215         {
216             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
217                 << (8 * sizeof(WORD_TYPE) - 8
218                      - p_bit_stream->fifo.i_available);
219             p_bit_stream->fifo.i_available += 8;
220         }
221         else
222         {
223             p_bit_stream->pf_next_data_packet( p_bit_stream );
224             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
225                 << (8 * sizeof(WORD_TYPE) - 8
226                      - p_bit_stream->fifo.i_available);
227             p_bit_stream->fifo.i_available += 8;
228         }
229     }
230 }
231
232 /*****************************************************************************
233  * ShowBits : return i_bits bits from the bit stream
234  *****************************************************************************/
235 static __inline__ u32 ShowBits( bit_stream_t * p_bit_stream,
236                                 unsigned int i_bits )
237 {
238     if( p_bit_stream->fifo.i_available >= i_bits )
239     {
240         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
241     }
242
243     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
244     {
245         return( (p_bit_stream->fifo.buffer |
246                     (WORD_AT( p_bit_stream->p_byte )
247                         >> p_bit_stream->fifo.i_available))
248                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
249     }
250
251     return UnalignedShowBits( p_bit_stream, i_bits );
252 }
253
254 /*****************************************************************************
255  * RemoveBits : removes i_bits bits from the bit buffer
256  *              XXX: do not use for 32 bits, see RemoveBits32
257  *****************************************************************************/
258 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream,
259                                    unsigned int i_bits )
260 {
261     p_bit_stream->fifo.i_available -= i_bits;
262
263     if( p_bit_stream->fifo.i_available >= 0 )
264     {
265         p_bit_stream->fifo.buffer <<= i_bits;
266         return;
267     }
268
269     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
270     {
271         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
272                                         << ( -p_bit_stream->fifo.i_available );
273         ((WORD_TYPE *)p_bit_stream->p_byte)++;
274         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
275         return;
276     }
277
278     UnalignedRemoveBits( p_bit_stream );
279 }
280
281 /*****************************************************************************
282  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
283  *                refill it)
284  *****************************************************************************/
285 #if (WORD_TYPE == u32)
286 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
287 {
288     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
289     {
290         if( p_bit_stream->fifo.i_available )
291         {
292             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
293                             << (32 - p_bit_stream->fifo.i_available);
294             ((WORD_TYPE *)p_bit_stream->p_byte)++;
295             return;
296         }
297
298         ((WORD_TYPE *)p_bit_stream->p_byte)++;
299         return;
300     }
301
302     p_bit_stream->fifo.i_available -= 32;
303     UnalignedRemoveBits( p_bit_stream );
304 }
305 #else
306 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
307 #endif
308
309 /*****************************************************************************
310  * GetBits : returns i_bits bits from the bit stream and removes them
311  *           XXX: do not use for 32 bits, see GetBits32
312  *****************************************************************************/
313 static __inline__ u32 GetBits( bit_stream_t * p_bit_stream,
314                                unsigned int i_bits )
315 {
316     u32             i_result;
317
318     p_bit_stream->fifo.i_available -= i_bits;
319
320     if( p_bit_stream->fifo.i_available >= 0 )
321     {
322         i_result = p_bit_stream->fifo.buffer
323                         >> (8 * sizeof(WORD_TYPE) - i_bits);
324         p_bit_stream->fifo.buffer <<= i_bits;
325         return( i_result );
326     }
327
328     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
329     {
330         i_result = p_bit_stream->fifo.buffer
331                         >> (8 * sizeof(WORD_TYPE) - i_bits);
332         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
333         ((WORD_TYPE *)p_bit_stream->p_byte)++;
334         i_result |= p_bit_stream->fifo.buffer
335                         >> (8 * sizeof(WORD_TYPE)
336                                      + p_bit_stream->fifo.i_available);
337         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
338         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
339         return( i_result );
340     }
341
342     return UnalignedGetBits( p_bit_stream, i_bits );
343 }
344
345 /*****************************************************************************
346  * GetBits32 : returns 32 bits from the bit stream and removes them
347  *****************************************************************************/
348 #if (WORD_TYPE == u32)
349 static __inline__ u32 GetBits32( bit_stream_t * p_bit_stream )
350 {
351     u32             i_result;
352
353     if( p_bit_stream->fifo.i_available == 32 )
354     {
355         p_bit_stream->fifo.i_available = 0;
356         i_result = p_bit_stream->fifo.buffer;
357         p_bit_stream->fifo.buffer = 0;
358         return( i_result );
359     }
360
361     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
362     {
363         if( p_bit_stream->fifo.i_available )
364         {
365             i_result = p_bit_stream->fifo.buffer;
366             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
367             ((WORD_TYPE *)p_bit_stream->p_byte)++;
368             i_result |= p_bit_stream->fifo.buffer
369                              >> (p_bit_stream->fifo.i_available);
370             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
371             return( i_result );
372         }
373
374         i_result = WORD_AT( p_bit_stream->p_byte );
375         ((WORD_TYPE *)p_bit_stream->p_byte)++;
376         return( i_result );
377     }
378
379     p_bit_stream->fifo.i_available -= 32;
380     return UnalignedGetBits( p_bit_stream, 32 );
381 }
382 #else
383 #   define GetBits32( p_bit_stream )    GetBits( p_bit_stream, 32 )
384 #endif
385
386 /*****************************************************************************
387  * RealignBits : realigns the bit buffer on an 8-bit boundary
388  *****************************************************************************/
389 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
390 {
391     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
392     p_bit_stream->fifo.i_available &= ~0x7;
393 }
394
395
396 /*****************************************************************************
397  * GetChunk : reads a large chunk of data
398  *****************************************************************************
399  * The position in the stream must be byte-aligned, if unsure call
400  * RealignBits(). p_buffer must to a buffer at least as big as i_buf_len
401  * otherwise your code will crash.
402  *****************************************************************************/
403 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
404                                  byte_t * p_buffer, size_t i_buf_len )
405 {
406     ptrdiff_t           i_available;
407
408     if( p_bit_stream->fifo.i_available )
409     {
410         *((WORD_TYPE *)p_buffer) = WORD_AT( &p_bit_stream->fifo.buffer );
411         p_buffer += p_bit_stream->fifo.i_available >> 3;
412         i_buf_len -= p_bit_stream->fifo.i_available >> 3;
413         p_bit_stream->fifo.buffer = 0;
414         p_bit_stream->fifo.i_available = 0;
415     }
416
417     if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
418             >= i_buf_len )
419     {
420         memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
421         p_bit_stream->p_byte += i_buf_len;
422     }
423     else
424     {
425         do
426         {
427             memcpy( p_buffer, p_bit_stream->p_byte, i_available );
428             p_bit_stream->p_byte = p_bit_stream->p_end;
429             p_buffer += i_available;
430             i_buf_len -= i_available;
431             p_bit_stream->pf_next_data_packet( p_bit_stream );
432         }
433         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
434                 <= i_buf_len && !p_bit_stream->p_decoder_fifo->b_die );
435
436         if( i_buf_len )
437         {
438             memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
439             p_bit_stream->p_byte += i_buf_len;
440         }
441     }
442
443     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
444     {
445         AlignWord( p_bit_stream );
446     }
447 }
448
449
450 /*
451  * The following functions are now deprecated.
452  */
453
454 static __inline__ byte_t _GetByte( bit_stream_t * p_bit_stream )
455 {
456     if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
457     {
458         p_bit_stream->pf_next_data_packet( p_bit_stream );
459     }
460
461     return( *(p_bit_stream->p_byte++) );
462 }
463
464 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
465 {
466     while ( p_bit_stream->fifo.i_available < i_bits )
467     {
468         p_bit_stream->fifo.buffer |= ((WORD_TYPE)_GetByte( p_bit_stream ))
469                                      << (8 * sizeof(WORD_TYPE) - 8
470                                             - p_bit_stream->fifo.i_available);
471         p_bit_stream->fifo.i_available += 8;
472     }
473 }
474
475 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
476 {
477     p_bit_stream->fifo.buffer <<= i_bits;
478     p_bit_stream->fifo.i_available -= i_bits;
479 }
480
481
482 /*
483  * Communication interface between input and decoders
484  */
485
486 /*****************************************************************************
487  * decoder_config_t
488  *****************************************************************************
489  * Standard pointers given to the decoders as a toolbox.
490  *****************************************************************************/
491 typedef struct decoder_config_s
492 {
493     u16                     i_id;
494     u8                      i_type;         /* type of the elementary stream */
495
496     struct stream_ctrl_s *  p_stream_ctrl;
497     struct decoder_fifo_s * p_decoder_fifo;
498     void                 (* pf_init_bit_stream)( struct bit_stream_s *,
499                                                  struct decoder_fifo_s * );
500 } decoder_config_t;
501
502 /*****************************************************************************
503  * vdec_config_t
504  *****************************************************************************
505  * Pointers given to video decoders threads.
506  *****************************************************************************/
507 struct vout_thread_s;
508
509 typedef struct vdec_config_s
510 {
511     struct vout_thread_s *  p_vout;
512
513     struct picture_s *   (* pf_create_picture)( struct vout_thread_s *,
514                                                 int i_type, int i_width,
515                                                 int i_height );
516     void                 (* pf_destroy_picture)( struct vout_thread_s *,
517                                                  struct picture_s * );
518     void                 (* pf_display_picture)( struct vout_thread_s *,
519                                                  struct picture_s * );
520     void                 (* pf_date_picture)( struct vout_thread_s *,
521                                               struct picture_s *, mtime_t date );
522     void                 (* pf_link_picture)( struct vout_thread_s *,
523                                               struct picture_s *, mtime_t date );
524     void                 (* pf_unlink_picture)( struct vout_thread_s *,
525                                                 struct picture_s *, mtime_t date );
526     struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
527                                                    int i_type, int i_size );
528     void                 (* pf_destroy_subpicture)( struct vout_thread_s *,
529                                                     struct subpicture_s * );
530     void                 (* pf_display_subpicture)( struct vout_thread_s *,
531                                                     struct subpicture_s * );
532
533     decoder_config_t        decoder_config;
534 } vdec_config_t;
535
536 /*****************************************************************************
537  * adec_config_t
538  *****************************************************************************
539  * Pointers given to audio decoders threads.
540  *****************************************************************************/
541 struct aout_thread_s;
542
543 typedef struct adec_config_s
544 {
545     struct aout_thread_s *  p_aout;
546
547     struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
548                                             struct aout_fifo_s * );
549     void                 (* pf_destroy_fifo)( struct aout_thread_s *);
550
551     decoder_config_t        decoder_config;
552 } adec_config_t;
553
554
555 /*
556  * Communication interface between decoders and input
557  */
558
559 /*****************************************************************************
560  * decoder_capabilities_t
561  *****************************************************************************
562  * Structure returned by a call to GetCapabilities() of the decoder.
563  *****************************************************************************/
564 typedef struct decoder_capabilities_s
565 {
566     int                     i_dec_type;
567     u8                      i_stream_type;   /* == i_type in es_descriptor_t */
568     int                     i_weight; /* for a given stream type, the decoder
569                                        * with higher weight will be spawned  */
570
571     vlc_thread_t         (* pf_create_thread)( void * );
572 } decoder_capabilities_t;
573
574 /* Decoder types */
575 #define NONE_D              0
576 #define VIDEO_D             1
577 #define AUDIO_D             2