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