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