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