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 $
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.
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.
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 *****************************************************************************/
24 /* Structures exported to the decoders */
26 /*****************************************************************************
28 *****************************************************************************
29 * Describe a data packet.
30 *****************************************************************************/
31 typedef struct data_packet_s
33 /* Nothing before this line, the code relies on that */
34 byte_t * p_buffer; /* raw data packet */
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 ? */
42 /* Used to chain the TS packets that carry data for a same PES or PSI */
43 struct data_packet_s * p_next;
46 /*****************************************************************************
48 *****************************************************************************
49 * Describes an PES packet, with its properties, and pointers to the TS packets
51 *****************************************************************************/
52 typedef struct pes_packet_s
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
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) */
67 int i_pes_size; /* size of the current PES packet */
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). */
75 /*****************************************************************************
77 *****************************************************************************
78 * This rotative FIFO contains PES packets that are to be decoded.
79 *****************************************************************************/
80 typedef struct decoder_fifo_s
82 /* Thread structures */
83 vlc_mutex_t data_lock; /* fifo data lock */
84 vlc_cond_t data_wait; /* fifo data conditional variable */
87 pes_packet_t * buffer[FIFO_SIZE + 1];
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 */
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)\
105 #define DECODER_FIFO_START( fifo ) ( (fifo).buffer[ (fifo).i_start ] )
106 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
108 #define DECODER_FIFO_END( fifo ) ( (fifo).buffer[ (fifo).i_end ] )
109 #define DECODER_FIFO_INCEND( fifo ) ( (fifo).i_end = ((fifo).i_end + 1) \
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;
120 typedef struct bit_fifo_s
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. */
127 /* Number of bits available in the bit buffer */
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
148 /* The decoder fifo contains the data of the PES stream */
149 decoder_fifo_t * p_decoder_fifo;
151 /* Function to jump to the next data packet */
152 void (* pf_next_data_packet)( struct bit_stream_s * );
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;
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) */
168 /* Pointer to the last byte that is to be read (in the current TS packet */
172 /*****************************************************************************
173 * Inline functions used by the decoders to read bit_stream_t
174 *****************************************************************************/
177 * DISCUSSION : How to use the bit_stream structures
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.
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.
188 #if (WORD_TYPE == u32)
189 # define WORD_AT U32_AT
190 #elif (WORD_TYPE == u64)
191 # define WORD_AT U64_AT
193 # error Unsupported WORD_TYPE
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 );
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 )
210 while( (p_bit_stream->p_byte - p_bit_stream->p_data->p_buffer)
211 & (sizeof(WORD_TYPE) - 1) )
213 if( p_bit_stream->p_byte < p_bit_stream->p_end )
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;
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;
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 )
237 if( p_bit_stream->fifo.i_available >= i_bits )
239 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
242 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
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) );
250 return UnalignedShowBits( p_bit_stream, i_bits );
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 )
260 p_bit_stream->fifo.i_available -= i_bits;
262 if( p_bit_stream->fifo.i_available >= 0 )
264 p_bit_stream->fifo.buffer <<= i_bits;
268 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
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;
277 UnalignedRemoveBits( p_bit_stream );
280 /*****************************************************************************
281 * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
283 *****************************************************************************/
284 #if (WORD_TYPE == u32)
285 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
287 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
289 if( p_bit_stream->fifo.i_available )
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)++;
297 ((WORD_TYPE *)p_bit_stream->p_byte)++;
301 p_bit_stream->fifo.i_available -= 32;
302 UnalignedRemoveBits( p_bit_stream );
305 # define RemoveBits32( p_bit_stream ) RemoveBits( p_bit_stream, 32 )
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 )
317 p_bit_stream->fifo.i_available -= i_bits;
319 if( p_bit_stream->fifo.i_available >= 0 )
321 i_result = p_bit_stream->fifo.buffer
322 >> (8 * sizeof(WORD_TYPE) - i_bits);
323 p_bit_stream->fifo.buffer <<= i_bits;
327 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
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;
341 return UnalignedGetBits( p_bit_stream, i_bits );
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 )
352 if( p_bit_stream->fifo.i_available == 32 )
354 p_bit_stream->fifo.i_available = 0;
355 i_result = p_bit_stream->fifo.buffer;
356 p_bit_stream->fifo.buffer = 0;
360 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
362 if( p_bit_stream->fifo.i_available )
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);
373 i_result = WORD_AT( p_bit_stream->p_byte );
374 ((WORD_TYPE *)p_bit_stream->p_byte)++;
378 p_bit_stream->fifo.i_available -= 32;
379 return UnalignedGetBits( p_bit_stream, 32 );
382 # define GetBits32( p_bit_stream ) GetBits( p_bit_stream, 32 )
385 /*****************************************************************************
386 * RealignBits : realigns the bit buffer on an 8-bit boundary
387 *****************************************************************************/
388 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
390 p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
391 p_bit_stream->fifo.i_available &= ~0x7;
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 )
405 ptrdiff_t i_available;
407 if( p_bit_stream->fifo.i_available )
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;
416 if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
419 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
420 p_bit_stream->p_byte += i_buf_len;
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 );
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 );
437 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
438 p_bit_stream->p_byte += i_buf_len;
442 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
444 AlignWord( p_bit_stream );
450 * The following functions are now deprecated.
453 static __inline__ byte_t _GetByte( bit_stream_t * p_bit_stream )
455 if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
457 p_bit_stream->pf_next_data_packet( p_bit_stream );
460 return( *(p_bit_stream->p_byte++) );
463 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
465 while ( p_bit_stream->fifo.i_available < i_bits )
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;
474 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
476 p_bit_stream->fifo.buffer <<= i_bits;
477 p_bit_stream->fifo.i_available -= i_bits;
482 * Communication interface between input and decoders
485 /*****************************************************************************
487 *****************************************************************************
488 * Standard pointers given to the decoders as a toolbox.
489 *****************************************************************************/
490 typedef struct decoder_config_s
493 u8 i_type; /* type of the elementary stream */
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 * );
501 /*****************************************************************************
503 *****************************************************************************
504 * Pointers given to video decoders threads.
505 *****************************************************************************/
506 struct vout_thread_s;
508 typedef struct vdec_config_s
510 struct vout_thread_s * p_vout;
512 struct picture_s * (* pf_create_picture)( struct vout_thread_s *,
513 int i_type, int i_width,
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 * );
532 decoder_config_t decoder_config;
535 /*****************************************************************************
537 *****************************************************************************
538 * Pointers given to audio decoders threads.
539 *****************************************************************************/
540 struct aout_thread_s;
542 typedef struct adec_config_s
544 struct aout_thread_s * p_aout;
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 *);
550 decoder_config_t decoder_config;
555 * Communication interface between decoders and input
558 /*****************************************************************************
559 * decoder_capabilities_t
560 *****************************************************************************
561 * Structure returned by a call to GetCapabilities() of the decoder.
562 *****************************************************************************/
563 typedef struct decoder_capabilities_s
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 */
570 vlc_thread_t (* pf_create_thread)( void * );
571 } decoder_capabilities_t;