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