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.35 2001/09/24 11:17:49 massiot Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8 * Michel Kaempf <maxx@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 /* Structures exported to the decoders */
27 /*****************************************************************************
29 *****************************************************************************
30 * Describe a data packet.
31 *****************************************************************************/
32 typedef struct data_packet_s
34 /* Nothing before this line, the code relies on that */
35 byte_t * p_buffer; /* raw data packet */
36 long l_size; /* buffer size */
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 ? */
46 /* Used to chain the TS packets that carry data for a same PES or PSI */
47 struct data_packet_s * p_next;
50 /*****************************************************************************
52 *****************************************************************************
53 * Describes an PES packet, with its properties, and pointers to the TS packets
55 *****************************************************************************/
56 typedef struct pes_packet_s
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
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) */
69 int i_pes_size; /* size of the current PES packet */
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). */
77 /*****************************************************************************
79 *****************************************************************************
80 * This rotative FIFO contains PES packets that are to be decoded.
81 *****************************************************************************/
82 typedef struct decoder_fifo_s
84 /* Thread structures */
85 vlc_mutex_t data_lock; /* fifo data lock */
86 vlc_cond_t data_wait; /* fifo data conditional variable */
89 pes_packet_t * buffer[FIFO_SIZE + 1];
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 */
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)\
107 #define DECODER_FIFO_START( fifo ) ( (fifo).buffer[ (fifo).i_start ] )
108 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
110 #define DECODER_FIFO_END( fifo ) ( (fifo).buffer[ (fifo).i_end ] )
111 #define DECODER_FIFO_INCEND( fifo ) ( (fifo).i_end = ((fifo).i_end + 1) \
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;
122 typedef struct bit_fifo_s
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. */
129 /* Number of bits available in the bit buffer */
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
150 /* The decoder fifo contains the data of the PES stream */
151 decoder_fifo_t * p_decoder_fifo;
153 /* Function to jump to the next data packet */
154 void (* pf_next_data_packet)( struct bit_stream_s * );
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;
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) */
170 /* Pointer to the last byte that is to be read (in the current packet */
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;
177 /*****************************************************************************
178 * Inline functions used by the decoders to read bit_stream_t
179 *****************************************************************************/
182 * DISCUSSION : How to use the bit_stream structures
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.
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.
193 #if (WORD_TYPE == u32)
194 # define WORD_AT U32_AT
195 # define WORD_SIGNED s32
196 #elif (WORD_TYPE == u64)
197 # define WORD_AT U64_AT
198 # define WORD_SIGNED s64
200 # error Unsupported WORD_TYPE
203 /*****************************************************************************
204 * Protoypes from input_ext-dec.c
205 *****************************************************************************/
206 void UnalignedShowBits( struct bit_stream_s *, unsigned int );
207 void UnalignedRemoveBits( struct bit_stream_s * );
208 u32 UnalignedGetBits( struct bit_stream_s *, unsigned int );
210 /*****************************************************************************
211 * AlignWord : fill in the bit buffer so that the byte pointer be aligned
212 * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
213 * empty bytes in the bit buffer)
214 *****************************************************************************/
215 static __inline__ void AlignWord( bit_stream_t * p_bit_stream )
217 while( (ptrdiff_t)p_bit_stream->p_byte
218 & (sizeof(WORD_TYPE) - 1) )
220 if( p_bit_stream->p_byte < p_bit_stream->p_end )
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;
229 p_bit_stream->pf_next_data_packet( p_bit_stream );
230 p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
231 << (8 * sizeof(WORD_TYPE) - 8
232 - p_bit_stream->fifo.i_available);
233 p_bit_stream->fifo.i_available += 8;
238 /*****************************************************************************
239 * ShowBits : return i_bits bits from the bit stream
240 *****************************************************************************/
241 static __inline__ u32 ShowBits( bit_stream_t * p_bit_stream,
242 unsigned int i_bits )
244 if( p_bit_stream->fifo.i_available >= i_bits )
246 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
249 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
251 return( (p_bit_stream->fifo.buffer |
252 (WORD_AT( p_bit_stream->p_byte )
253 >> p_bit_stream->fifo.i_available))
254 >> (8 * sizeof(WORD_TYPE) - i_bits) );
257 UnalignedShowBits( p_bit_stream, i_bits );
259 /* Now that we have filled in the buffers, do it again. Can't call
260 * ShowBits() instead, because the function is inline... */
261 if( p_bit_stream->fifo.i_available >= i_bits )
263 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
266 return( (p_bit_stream->fifo.buffer |
267 (WORD_AT( p_bit_stream->p_byte )
268 >> p_bit_stream->fifo.i_available))
269 >> (8 * sizeof(WORD_TYPE) - i_bits) );
272 /*****************************************************************************
273 * ShowSignedBits : return i_bits bits from the bit stream, using signed
275 *****************************************************************************/
276 static __inline__ s32 ShowSignedBits( bit_stream_t * p_bit_stream,
277 unsigned int i_bits )
279 if( p_bit_stream->fifo.i_available >= i_bits )
281 return( (WORD_SIGNED)p_bit_stream->fifo.buffer
282 >> (8 * sizeof(WORD_TYPE) - i_bits) );
285 /* You can probably do something a little faster, but now I'm tired. */
286 return( (WORD_SIGNED)(ShowBits( p_bit_stream, i_bits ) << (32 - i_bits))
290 /*****************************************************************************
291 * RemoveBits : removes i_bits bits from the bit buffer
292 * XXX: do not use for 32 bits, see RemoveBits32
293 *****************************************************************************/
294 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream,
295 unsigned int i_bits )
297 p_bit_stream->fifo.i_available -= i_bits;
299 if( p_bit_stream->fifo.i_available >= 0 )
301 p_bit_stream->fifo.buffer <<= i_bits;
305 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
307 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
308 << ( -p_bit_stream->fifo.i_available );
309 ((WORD_TYPE *)p_bit_stream->p_byte)++;
310 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
314 UnalignedRemoveBits( p_bit_stream );
317 /*****************************************************************************
318 * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
320 *****************************************************************************/
321 #if (WORD_TYPE == u32)
322 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
324 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
326 if( p_bit_stream->fifo.i_available )
328 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
329 << (32 - p_bit_stream->fifo.i_available);
330 ((WORD_TYPE *)p_bit_stream->p_byte)++;
334 ((WORD_TYPE *)p_bit_stream->p_byte)++;
338 p_bit_stream->fifo.i_available -= 32;
339 UnalignedRemoveBits( p_bit_stream );
342 # define RemoveBits32( p_bit_stream ) RemoveBits( p_bit_stream, 32 )
345 /*****************************************************************************
346 * GetBits : returns i_bits bits from the bit stream and removes them
347 * XXX: do not use for 32 bits, see GetBits32
348 *****************************************************************************/
349 static __inline__ u32 GetBits( bit_stream_t * p_bit_stream,
350 unsigned int i_bits )
354 p_bit_stream->fifo.i_available -= i_bits;
356 if( p_bit_stream->fifo.i_available >= 0 )
358 i_result = p_bit_stream->fifo.buffer
359 >> (8 * sizeof(WORD_TYPE) - i_bits);
360 p_bit_stream->fifo.buffer <<= i_bits;
364 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
366 i_result = p_bit_stream->fifo.buffer
367 >> (8 * sizeof(WORD_TYPE) - i_bits);
368 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
369 ((WORD_TYPE *)p_bit_stream->p_byte)++;
370 i_result |= p_bit_stream->fifo.buffer
371 >> (8 * sizeof(WORD_TYPE)
372 + p_bit_stream->fifo.i_available);
373 p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
374 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
378 return UnalignedGetBits( p_bit_stream, i_bits );
381 /*****************************************************************************
382 * GetSignedBits : returns i_bits bits from the bit stream and removes them,
383 * using signed arithmetic
384 * XXX: do not use for 32 bits
385 *****************************************************************************/
386 static __inline__ s32 GetSignedBits( bit_stream_t * p_bit_stream,
387 unsigned int i_bits )
389 if( p_bit_stream->fifo.i_available >= i_bits )
393 p_bit_stream->fifo.i_available -= i_bits;
394 i_result = (WORD_SIGNED)p_bit_stream->fifo.buffer
395 >> (8 * sizeof(WORD_TYPE) - i_bits);
396 p_bit_stream->fifo.buffer <<= i_bits;
400 /* You can probably do something a little faster, but now I'm tired. */
401 return( (WORD_SIGNED)(GetBits( p_bit_stream, i_bits ) << (32 - i_bits))
405 /*****************************************************************************
406 * GetBits32 : returns 32 bits from the bit stream and removes them
407 *****************************************************************************/
408 #if (WORD_TYPE == u32)
409 static __inline__ u32 GetBits32( bit_stream_t * p_bit_stream )
413 if( p_bit_stream->fifo.i_available == 32 )
415 p_bit_stream->fifo.i_available = 0;
416 i_result = p_bit_stream->fifo.buffer;
417 p_bit_stream->fifo.buffer = 0;
421 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
423 if( p_bit_stream->fifo.i_available )
425 i_result = p_bit_stream->fifo.buffer;
426 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
427 ((WORD_TYPE *)p_bit_stream->p_byte)++;
428 i_result |= p_bit_stream->fifo.buffer
429 >> (p_bit_stream->fifo.i_available);
430 p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
434 i_result = WORD_AT( p_bit_stream->p_byte );
435 ((WORD_TYPE *)p_bit_stream->p_byte)++;
439 p_bit_stream->fifo.i_available -= 32;
440 return UnalignedGetBits( p_bit_stream, 32 );
443 # define GetBits32( p_bit_stream ) GetBits( p_bit_stream, 32 )
446 /*****************************************************************************
447 * RealignBits : realigns the bit buffer on an 8-bit boundary
448 *****************************************************************************/
449 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
451 p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
452 p_bit_stream->fifo.i_available &= ~0x7;
456 /*****************************************************************************
457 * GetChunk : reads a large chunk of data
458 *****************************************************************************
459 * The position in the stream must be byte-aligned, if unsure call
460 * RealignBits(). p_buffer must point to a buffer at least as big as i_buf_len
461 * otherwise your code will crash.
462 *****************************************************************************/
463 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
464 byte_t * p_buffer, size_t i_buf_len )
466 ptrdiff_t i_available;
468 /* We need to take care because i_buf_len may be < 4. */
469 while( p_bit_stream->fifo.i_available > 0 && i_buf_len )
471 *p_buffer = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - 8);
474 p_bit_stream->fifo.buffer <<= 8;
475 p_bit_stream->fifo.i_available -= 8;
478 if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
481 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
482 p_bit_stream->p_byte += i_buf_len;
488 memcpy( p_buffer, p_bit_stream->p_byte, i_available );
489 p_bit_stream->p_byte = p_bit_stream->p_end;
490 p_buffer += i_available;
491 i_buf_len -= i_available;
492 p_bit_stream->pf_next_data_packet( p_bit_stream );
494 while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
495 <= i_buf_len && !p_bit_stream->p_decoder_fifo->b_die );
499 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
500 p_bit_stream->p_byte += i_buf_len;
504 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
506 AlignWord( p_bit_stream );
512 * The following functions are now deprecated.
515 static __inline__ byte_t _GetByte( bit_stream_t * p_bit_stream )
517 if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
519 p_bit_stream->pf_next_data_packet( p_bit_stream );
522 return( *(p_bit_stream->p_byte++) );
525 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
527 while ( p_bit_stream->fifo.i_available < i_bits )
529 p_bit_stream->fifo.buffer |= ((WORD_TYPE)_GetByte( p_bit_stream ))
530 << (8 * sizeof(WORD_TYPE) - 8
531 - p_bit_stream->fifo.i_available);
532 p_bit_stream->fifo.i_available += 8;
536 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
538 p_bit_stream->fifo.buffer <<= i_bits;
539 p_bit_stream->fifo.i_available -= i_bits;
544 * Communication interface between input and decoders
547 /*****************************************************************************
549 *****************************************************************************
550 * Standard pointers given to the decoders as a toolbox.
551 *****************************************************************************/
552 typedef struct decoder_config_s
555 u8 i_type; /* type of the elementary stream */
557 struct stream_ctrl_s * p_stream_ctrl;
558 struct decoder_fifo_s * p_decoder_fifo;
559 void (* pf_init_bit_stream)( struct bit_stream_s *,
560 struct decoder_fifo_s *,
561 void (* pf_bitstream_callback)( struct bit_stream_s *,
566 /*****************************************************************************
568 *****************************************************************************
569 * Pointers given to video decoders threads.
570 *****************************************************************************/
571 struct vout_thread_s;
573 typedef struct vdec_config_s
575 struct picture_s * (* pf_create_picture)( struct vout_thread_s *,
576 int i_type, int i_width,
578 void (* pf_destroy_picture)( struct vout_thread_s *,
579 struct picture_s * );
580 void (* pf_display_picture)( struct vout_thread_s *,
581 struct picture_s * );
582 void (* pf_date_picture)( struct vout_thread_s *,
583 struct picture_s *, mtime_t date );
584 void (* pf_link_picture)( struct vout_thread_s *,
585 struct picture_s *, mtime_t date );
586 void (* pf_unlink_picture)( struct vout_thread_s *,
587 struct picture_s *, mtime_t date );
588 struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
589 int i_type, int i_size );
590 void (* pf_destroy_subpicture)( struct vout_thread_s *,
591 struct subpicture_s * );
592 void (* pf_display_subpicture)( struct vout_thread_s *,
593 struct subpicture_s * );
595 decoder_config_t decoder_config;
598 /*****************************************************************************
600 *****************************************************************************
601 * Pointers given to audio decoders threads.
602 *****************************************************************************/
603 typedef struct adec_config_s
605 struct aout_fifo_s * (* pf_create_fifo)( struct aout_fifo_s * );
606 void (* pf_destroy_fifo)( void );
608 decoder_config_t decoder_config;
613 * Communication interface between decoders and input
616 /*****************************************************************************
617 * decoder_capabilities_t
618 *****************************************************************************
619 * Structure returned by a call to GetCapabilities() of the decoder.
620 *****************************************************************************/
621 typedef struct decoder_capabilities_s
624 u8 i_stream_type; /* == i_type in es_descriptor_t */
625 int i_weight; /* for a given stream type, the decoder
626 * with higher weight will be spawned */
628 vlc_thread_t (* pf_create_thread)( void * );
629 } decoder_capabilities_t;