]> git.sesse.net Git - vlc/blob - include/input_ext-dec.h
12c8ceadb4e046130f476317c3cfe7a660a7c47d
[vlc] / include / input_ext-dec.h
1 /*****************************************************************************
2  * input_ext-dec.h: structures exported to the VideoLAN decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: input_ext-dec.h,v 1.80 2003/09/02 20:19:25 gbazin 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 #ifndef _VLC_INPUT_EXT_DEC_H
26 #define _VLC_INPUT_EXT_DEC_H 1
27
28 /* Structures exported to the decoders */
29
30 /*****************************************************************************
31  * data_packet_t
32  *****************************************************************************
33  * Describe a data packet.
34  *****************************************************************************/
35 struct data_packet_t
36 {
37     /* Used to chain the packets that carry data for a same PES or PSI */
38     data_packet_t *  p_next;
39
40     /* start of the PS or TS packet */
41     byte_t *         p_demux_start;
42     /* start of the PES payload in this packet */
43     byte_t *         p_payload_start;
44     byte_t *         p_payload_end; /* guess ? :-) */
45     /* is the packet messed up ? */
46     vlc_bool_t       b_discard_payload;
47
48     /* pointer to the real data */
49     data_buffer_t *  p_buffer;
50 };
51
52 /*****************************************************************************
53  * pes_packet_t
54  *****************************************************************************
55  * Describes an PES packet, with its properties, and pointers to the TS packets
56  * containing it.
57  *****************************************************************************/
58 struct pes_packet_t
59 {
60     /* Chained list to the next PES packet (depending on the context) */
61     pes_packet_t *  p_next;
62
63     /* PES properties */
64     vlc_bool_t      b_data_alignment;          /* used to find the beginning of
65                                                 * a video or audio unit */
66     vlc_bool_t      b_discontinuity;          /* This packet doesn't follow the
67                                                * previous one */
68
69     mtime_t         i_pts;            /* PTS for this packet (zero if unset) */
70     mtime_t         i_dts;            /* DTS for this packet (zero if unset) */
71     int             i_rate;   /* current reading pace (see stream_control.h) */
72
73     unsigned int    i_pes_size;            /* size of the current PES packet */
74
75     /* Chained list to packets */
76     data_packet_t * p_first;              /* The first packet contained by this
77                                            * PES (used by decoders). */
78     data_packet_t * p_last;            /* The last packet contained by this
79                                         * PES (used by the buffer allocator) */
80     unsigned int    i_nb_data; /* Number of data packets in the chained list */
81 };
82
83 /*****************************************************************************
84  * decoder_fifo_t
85  *****************************************************************************
86  * This rotative FIFO contains PES packets that are to be decoded.
87  *****************************************************************************/
88 struct decoder_fifo_t
89 {
90     VLC_COMMON_MEMBERS
91
92     /* Thread structures */
93     vlc_mutex_t         data_lock;                         /* fifo data lock */
94     vlc_cond_t          data_wait;         /* fifo data conditional variable */
95
96     /* Data */
97     pes_packet_t *      p_first;
98     pes_packet_t **     pp_last;
99     int                 i_depth;       /* number of PES packets in the stack */
100
101     /* Communication interface between input and decoders */
102     input_buffers_t    *p_packets_mgt;   /* packets management services data */
103
104     /* Standard pointers given to the decoders as a toolbox. */
105     uint16_t            i_id;
106     vlc_fourcc_t        i_fourcc;
107     es_sys_t *          p_demux_data;
108     stream_ctrl_t *     p_stream_ctrl;
109     sout_instance_t *   p_sout;
110     void *              p_waveformatex;
111     void *              p_bitmapinfoheader;
112
113     decoder_t *         p_dec;
114 };
115
116 /*****************************************************************************
117  * decoder_t
118  *****************************************************************************
119  * The decoder descriptor.
120  *****************************************************************************/
121 struct decoder_t
122 {
123     VLC_COMMON_MEMBERS
124
125     /* Module properties */
126     module_t *          p_module;
127     decoder_sys_t *     p_sys;
128     int                 ( * pf_init )  ( decoder_t * );
129     int                 ( * pf_decode )( decoder_t *, block_t * );
130     int                 ( * pf_end )   ( decoder_t * );
131
132     /* Input properties */
133     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
134
135     /* Tmp field for old decoder api */
136     int                 ( * pf_run ) ( decoder_fifo_t * );
137 };
138
139 /*****************************************************************************
140  * bit_fifo_t : bit fifo descriptor
141  *****************************************************************************
142  * This type describes a bit fifo used to store bits while working with the
143  * input stream at the bit level.
144  *****************************************************************************/
145 typedef uint32_t    WORD_TYPE;
146
147 typedef struct bit_fifo_t
148 {
149     /* This unsigned integer allows us to work at the bit level. This buffer
150      * can contain 32 bits, and the used space can be found on the MSb's side
151      * and the available space on the LSb's side. */
152     WORD_TYPE           buffer;
153
154     /* Number of bits available in the bit buffer */
155     int                 i_available;
156
157 } bit_fifo_t;
158
159 /*****************************************************************************
160  * bit_stream_t : bit stream descriptor
161  *****************************************************************************
162  * This type, based on a PES stream, includes all the structures needed to
163  * handle the input stream like a bit stream.
164  *****************************************************************************/
165 struct bit_stream_t
166 {
167     /*
168      * Bit structures
169      */
170     bit_fifo_t       fifo;
171
172     /*
173      * Input structures
174      */
175     /* The decoder fifo contains the data of the PES stream */
176     decoder_fifo_t * p_decoder_fifo;
177
178     /* Callback to the decoder used when changing data packets ; set
179      * to NULL if your decoder doesn't need it. */
180     void          (* pf_bitstream_callback)( bit_stream_t *, vlc_bool_t );
181     /* Optional argument to the callback */
182     void *           p_callback_arg;
183
184     /*
185      * PTS retrieval
186      */
187     mtime_t          i_pts, i_dts;
188     byte_t *         p_pts_validity;
189
190     /*
191      * Byte structures
192      */
193     /* Current PES packet (extracted from the PES stream) */
194     pes_packet_t *          p_pes;
195     /* Current data packet (in the current PES packet) */
196     data_packet_t *         p_data;
197     /* Pointer to the next byte that is to be read (in the current packet) */
198     byte_t *                p_byte;
199     /* Pointer to the last byte that is to be read (in the current packet */
200     byte_t *                p_end;
201     /* Temporary buffer in case we're not aligned when changing data packets */
202     WORD_TYPE               i_showbits_buffer;
203     data_packet_t           showbits_data;
204 };
205
206 /*****************************************************************************
207  * Inline functions used by the decoders to read bit_stream_t
208  *****************************************************************************/
209
210 /*
211  * DISCUSSION : How to use the bit_stream structures
212  *
213  * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
214  * minimizing the number of p_byte changes.
215  * Bits are read via GetBits() or ShowBits.
216  *
217  * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
218  * the data have to be already aligned on an 8-bit boundary, or wrong
219  * results will be returned. Use RealignBits() if unsure.
220  */
221
222 #if (WORD_TYPE == uint32_t)
223 #   define WORD_AT      U32_AT
224 #   define WORD_SIGNED  int32_t
225 #elif (WORD_TYPE == uint64_t)
226 #   define WORD_AT      U64_AT
227 #   define WORD_SIGNED  int64_t
228 #else
229 #   error Unsupported WORD_TYPE
230 #endif
231
232 /*****************************************************************************
233  * Prototypes from input_ext-dec.c
234  *****************************************************************************/
235 VLC_EXPORT( void, input_ExtractPES,        ( decoder_fifo_t *, pes_packet_t ** ) );
236 VLC_EXPORT( void, input_DeletePES,         ( input_buffers_t *, pes_packet_t * ) );
237 VLC_EXPORT( int, InitBitstream, ( bit_stream_t *, decoder_fifo_t *, void ( * )( bit_stream_t *, vlc_bool_t ), void * p_callback_arg ) );
238 VLC_EXPORT( vlc_bool_t, NextDataPacket,    ( decoder_fifo_t *, bit_stream_t * ) );
239 VLC_EXPORT( void, BitstreamNextDataPacket, ( bit_stream_t * ) );
240 VLC_EXPORT( uint32_t, UnalignedShowBits,   ( bit_stream_t *, unsigned int ) );
241 VLC_EXPORT( void, UnalignedRemoveBits,     ( bit_stream_t * ) );
242 VLC_EXPORT( uint32_t, UnalignedGetBits,    ( bit_stream_t *, unsigned int ) );
243 VLC_EXPORT( void, CloseBitstream,          ( bit_stream_t * ) );
244 VLC_EXPORT( void, CurrentPTS,              ( bit_stream_t *, mtime_t *, mtime_t * ) );
245 VLC_EXPORT( void, NextPTS,                 ( bit_stream_t *, mtime_t *, mtime_t * ) );
246
247 /*****************************************************************************
248  * AlignWord : fill in the bit buffer so that the byte pointer be aligned
249  * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
250  * empty bytes in the bit buffer)
251  *****************************************************************************/
252 static inline void AlignWord( bit_stream_t * p_bit_stream )
253 {
254     while( (ptrdiff_t)p_bit_stream->p_byte
255              & (sizeof(WORD_TYPE) - 1) )
256     {
257         if( p_bit_stream->p_byte < p_bit_stream->p_end )
258         {
259             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
260                 << (8 * sizeof(WORD_TYPE) - 8
261                      - p_bit_stream->fifo.i_available);
262             p_bit_stream->fifo.i_available += 8;
263         }
264         else
265         {
266             BitstreamNextDataPacket( p_bit_stream );
267             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
268                 << (8 * sizeof(WORD_TYPE) - 8
269                      - p_bit_stream->fifo.i_available);
270             p_bit_stream->fifo.i_available += 8;
271         }
272     }
273 }
274
275 /*****************************************************************************
276  * ShowBits : return i_bits bits from the bit stream
277  *****************************************************************************/
278 static inline uint32_t ShowBits( bit_stream_t * p_bit_stream,
279                                  unsigned int i_bits )
280 {
281     if( (unsigned int)p_bit_stream->fifo.i_available >= i_bits )
282     {
283         return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
284     }
285
286     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
287     {
288         return( (p_bit_stream->fifo.buffer |
289                     (WORD_AT( p_bit_stream->p_byte )
290                         >> p_bit_stream->fifo.i_available))
291                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
292     }
293
294     return( UnalignedShowBits( p_bit_stream, i_bits ) );
295 }
296
297 /*****************************************************************************
298  * ShowSignedBits : return i_bits bits from the bit stream, using signed
299  *                  arithmetic
300  *****************************************************************************/
301 static inline int32_t ShowSignedBits( bit_stream_t * p_bit_stream,
302                                       unsigned int i_bits )
303 {
304     if( (unsigned int)p_bit_stream->fifo.i_available >= i_bits )
305     {
306         return( (WORD_SIGNED)p_bit_stream->fifo.buffer
307                     >> (8 * sizeof(WORD_TYPE) - i_bits) );
308     }
309
310     /* You can probably do something a little faster, but now I'm tired. */
311     return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
312              >> (32 - i_bits) );
313 }
314
315 /*****************************************************************************
316  * RemoveBits : removes i_bits bits from the bit buffer
317  *              XXX: do not use for 32 bits, see RemoveBits32
318  *****************************************************************************/
319 static inline void RemoveBits( bit_stream_t * p_bit_stream,
320                                unsigned int i_bits )
321 {
322     p_bit_stream->fifo.i_available -= i_bits;
323
324     if( p_bit_stream->fifo.i_available >= 0 )
325     {
326         p_bit_stream->fifo.buffer <<= i_bits;
327         return;
328     }
329
330     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
331     {
332         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
333                                         << ( -p_bit_stream->fifo.i_available );
334         p_bit_stream->p_byte =
335                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
336         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
337         return;
338     }
339
340     UnalignedRemoveBits( p_bit_stream );
341 }
342
343 /*****************************************************************************
344  * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
345  *                refill it)
346  *****************************************************************************/
347 #if (WORD_TYPE == uint32_t)
348 static inline void RemoveBits32( bit_stream_t * p_bit_stream )
349 {
350     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
351     {
352         if( p_bit_stream->fifo.i_available )
353         {
354             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
355                             << (32 - p_bit_stream->fifo.i_available);
356             p_bit_stream->p_byte =
357                        (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
358             return;
359         }
360
361         p_bit_stream->p_byte =
362                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
363         return;
364     }
365
366     p_bit_stream->fifo.i_available -= 32;
367     UnalignedRemoveBits( p_bit_stream );
368 }
369 #else
370 #   define RemoveBits32( p_bit_stream )     RemoveBits( p_bit_stream, 32 )
371 #endif
372
373 /*****************************************************************************
374  * GetBits : returns i_bits bits from the bit stream and removes them
375  *           XXX: do not use for 32 bits, see GetBits32
376  *****************************************************************************/
377 static inline uint32_t GetBits( bit_stream_t * p_bit_stream,
378                                 unsigned int i_bits )
379 {
380     uint32_t        i_result;
381
382     p_bit_stream->fifo.i_available -= i_bits;
383
384     if( p_bit_stream->fifo.i_available >= 0 )
385     {
386         i_result = p_bit_stream->fifo.buffer
387                         >> (8 * sizeof(WORD_TYPE) - i_bits);
388         p_bit_stream->fifo.buffer <<= i_bits;
389         return( i_result );
390     }
391
392     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
393     {
394         i_result = p_bit_stream->fifo.buffer
395                         >> (8 * sizeof(WORD_TYPE) - i_bits);
396         p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
397         p_bit_stream->p_byte =
398                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
399         i_result |= p_bit_stream->fifo.buffer
400                         >> (8 * sizeof(WORD_TYPE)
401                                      + p_bit_stream->fifo.i_available);
402         p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
403         p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
404         return( i_result );
405     }
406
407     return UnalignedGetBits( p_bit_stream, i_bits );
408 }
409
410 /*****************************************************************************
411  * GetSignedBits : returns i_bits bits from the bit stream and removes them,
412  *                 using signed arithmetic
413  *                 XXX: do not use for 32 bits
414  *****************************************************************************/
415 static inline int32_t GetSignedBits( bit_stream_t * p_bit_stream,
416                                      unsigned int i_bits )
417 {
418     if( (unsigned int)p_bit_stream->fifo.i_available >= i_bits )
419     {
420         int32_t         i_result;
421
422         p_bit_stream->fifo.i_available -= i_bits;
423         i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
424                         >> (8 * sizeof(WORD_TYPE) - i_bits);
425         p_bit_stream->fifo.buffer <<= i_bits;
426         return( i_result );
427     }
428
429     /* You can probably do something a little faster, but now I'm tired. */
430     return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
431              >> (32 - i_bits) );
432 }
433
434 /*****************************************************************************
435  * GetBits32 : returns 32 bits from the bit stream and removes them
436  *****************************************************************************/
437 #if (WORD_TYPE == uint32_t)
438 static inline uint32_t GetBits32( bit_stream_t * p_bit_stream )
439 {
440     uint32_t        i_result;
441
442     if( p_bit_stream->fifo.i_available == 32 )
443     {
444         p_bit_stream->fifo.i_available = 0;
445         i_result = p_bit_stream->fifo.buffer;
446         p_bit_stream->fifo.buffer = 0;
447         return( i_result );
448     }
449
450     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
451     {
452         if( p_bit_stream->fifo.i_available )
453         {
454             i_result = p_bit_stream->fifo.buffer;
455             p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
456             p_bit_stream->p_byte =
457                        (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
458             i_result |= p_bit_stream->fifo.buffer
459                              >> (p_bit_stream->fifo.i_available);
460             p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
461             return( i_result );
462         }
463
464         i_result = WORD_AT( p_bit_stream->p_byte );
465         p_bit_stream->p_byte =
466                    (byte_t *) ( ((WORD_TYPE *)p_bit_stream->p_byte) + 1 );
467         return( i_result );
468     }
469
470     p_bit_stream->fifo.i_available -= 32;
471     return UnalignedGetBits( p_bit_stream, 32 );
472 }
473 #else
474 #   define GetBits32( p_bit_stream )    GetBits( p_bit_stream, 32 )
475 #endif
476
477 /*****************************************************************************
478  * RealignBits : realigns the bit buffer on an 8-bit boundary
479  *****************************************************************************/
480 static inline void RealignBits( bit_stream_t * p_bit_stream )
481 {
482     p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
483     p_bit_stream->fifo.i_available &= ~0x7;
484 }
485
486
487 /*****************************************************************************
488  * GetChunk : reads a large chunk of data
489  *****************************************************************************
490  * The position in the stream must be byte-aligned, if unsure call
491  * RealignBits(). p_buffer must point to a buffer at least as big as i_buf_len
492  * otherwise your code will crash.
493  *****************************************************************************/
494 static inline void GetChunk( bit_stream_t * p_bit_stream,
495                              byte_t * p_buffer, size_t i_buf_len )
496 {
497     ptrdiff_t           i_available;
498
499     /* We need to take care because i_buf_len may be < 4. */
500     while( p_bit_stream->fifo.i_available > 0 && i_buf_len )
501     {
502         *p_buffer = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - 8);
503         p_buffer++;
504         i_buf_len--;
505         p_bit_stream->fifo.buffer <<= 8;
506         p_bit_stream->fifo.i_available -= 8;
507     }
508
509     i_available = p_bit_stream->p_end - p_bit_stream->p_byte;
510     if( i_available >= (ptrdiff_t)i_buf_len )
511     {
512         p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
513                                            p_bit_stream->p_byte, i_buf_len );
514         p_bit_stream->p_byte += i_buf_len;
515     }
516     else
517     {
518         do
519         {
520             p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
521                                            p_bit_stream->p_byte, i_available );
522             p_bit_stream->p_byte = p_bit_stream->p_end;
523             p_buffer += i_available;
524             i_buf_len -= i_available;
525             BitstreamNextDataPacket( p_bit_stream );
526             if( p_bit_stream->p_decoder_fifo->b_die )
527                 return;
528         }
529         while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
530                 <= (ptrdiff_t)i_buf_len );
531
532         if( i_buf_len )
533         {
534             p_bit_stream->p_decoder_fifo->p_vlc->pf_memcpy( p_buffer,
535                                            p_bit_stream->p_byte, i_buf_len );
536             p_bit_stream->p_byte += i_buf_len;
537         }
538     }
539
540     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
541     {
542         AlignWord( p_bit_stream );
543     }
544 }
545
546
547 /*
548  * Communication interface between input and decoders
549  */
550
551 /*****************************************************************************
552  * Prototypes from input_dec.c
553  *****************************************************************************/
554 VLC_EXPORT( void, DecoderError, ( decoder_fifo_t * p_fifo ) );
555
556 #endif /* "input_ext-dec.h" */