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.9 2001/01/10 16:24:11 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;/* the PTS for this packet (zero if unset) */
63 mtime_t i_dts;/* the DTS for this packet (zero if unset) */
65 int i_pes_size; /* size of the current PES packet */
67 /* Pointers to packets (packets are then linked by the p_prev and
68 p_next fields of the data_packet_t struct) */
69 data_packet_t * p_first; /* The first packet containing this
70 * PES (used by decoders). */
73 /*****************************************************************************
75 *****************************************************************************
76 * This rotative FIFO contains PES packets that are to be decoded.
77 *****************************************************************************/
78 typedef struct decoder_fifo_s
80 /* Thread structures */
81 vlc_mutex_t data_lock; /* fifo data lock */
82 vlc_cond_t data_wait; /* fifo data conditional variable */
85 pes_packet_t * buffer[FIFO_SIZE + 1];
89 /* Communication interface between input and decoders */
90 boolean_t b_die; /* the decoder should return now */
91 boolean_t b_error; /* the decoder is in an error loop */
92 void * p_packets_mgt; /* packets management services
93 * data (netlist...) */
94 void (* pf_delete_pes)( void *, pes_packet_t * );
95 /* function to use when releasing a PES */
98 /* Macros to manage a decoder_fifo_t structure. Please remember to take
99 * data_lock before using them. */
100 #define DECODER_FIFO_ISEMPTY( fifo ) ( (fifo).i_start == (fifo).i_end )
101 #define DECODER_FIFO_ISFULL( fifo ) ( ( ((fifo).i_end + 1 - (fifo).i_start)\
103 #define DECODER_FIFO_START( fifo ) ( (fifo).buffer[ (fifo).i_start ] )
104 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
106 #define DECODER_FIFO_END( fifo ) ( (fifo).buffer[ (fifo).i_end ] )
107 #define DECODER_FIFO_INCEND( fifo ) ( (fifo).i_end = ((fifo).i_end + 1) \
110 /*****************************************************************************
111 * bit_fifo_t : bit fifo descriptor
112 *****************************************************************************
113 * This type describes a bit fifo used to store bits while working with the
114 * input stream at the bit level.
115 *****************************************************************************/
116 typedef u32 WORD_TYPE; /* only u32 is supported at the moment */
118 typedef struct bit_fifo_s
120 /* This unsigned integer allows us to work at the bit level. This buffer
121 * can contain 32 bits, and the used space can be found on the MSb's side
122 * and the available space on the LSb's side. */
125 /* Number of bits available in the bit buffer */
130 /*****************************************************************************
131 * bit_stream_t : bit stream descriptor
132 *****************************************************************************
133 * This type, based on a PES stream, includes all the structures needed to
134 * handle the input stream like a bit stream.
135 *****************************************************************************/
136 typedef struct bit_stream_s
141 /* The decoder fifo contains the data of the PES stream */
142 decoder_fifo_t * p_decoder_fifo;
144 /* Function to jump to the next data packet */
145 void (* pf_next_data_packet)( struct bit_stream_s * );
147 /* Callback to the decoder used when changing data packets ; set
148 * to NULL if your decoder doesn't need it. */
149 void (* pf_bitstream_callback)( struct bit_stream_s * );
150 /* Optional argument to the callback */
151 void * p_callback_arg;
156 /* Current data packet (in the current PES packet of the PES stream) */
157 data_packet_t * p_data;
158 /* Pointer to the next byte that is to be read (in the current TS packet) */
160 /* Pointer to the last byte that is to be read (in the current TS packet */
169 /*****************************************************************************
170 * Inline functions used by the decoders to read bit_stream_t
171 *****************************************************************************/
174 * Philosophy of the first implementation : the bit buffer is first filled by
175 * NeedBits, then the buffer can be read via p_bit_stream->fifo.buffer, and
176 * unnecessary bits are dumped with a DumpBits() call.
179 /*****************************************************************************
180 * GetByte : reads the next byte in the input stream
181 *****************************************************************************/
182 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
184 /* Are there some bytes left in the current data packet ? */
185 /* could change this test to have a if (! (bytes--)) instead */
186 if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
188 /* no, switch to next data packet */
189 p_bit_stream->pf_next_data_packet( p_bit_stream );
192 return( *(p_bit_stream->p_byte++) );
195 /*****************************************************************************
196 * NeedBits : reads i_bits new bits in the bit stream and stores them in the
198 *****************************************************************************
199 * - i_bits must be less or equal 32 !
200 * - There is something important to notice with that function : if the number
201 * of bits available in the bit buffer when calling NeedBits() is greater than
202 * 24 (i_available > 24) but less than the number of needed bits
203 * (i_available < i_bits), the byte returned by GetByte() will be shifted with
204 * a negative value and the number of bits available in the bit buffer will be
205 * set to more than 32 !
206 *****************************************************************************/
207 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
209 while ( p_bit_stream->fifo.i_available < i_bits )
211 p_bit_stream->fifo.buffer |= ((WORD_TYPE)GetByte( p_bit_stream ))
212 << (sizeof(WORD_TYPE) - 8
213 - p_bit_stream->fifo.i_available);
214 p_bit_stream->fifo.i_available += 8;
218 /*****************************************************************************
219 * DumpBits : removes i_bits bits from the bit buffer
220 *****************************************************************************
221 * - i_bits <= i_available
222 * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
223 *****************************************************************************/
224 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
226 p_bit_stream->fifo.buffer <<= i_bits;
227 p_bit_stream->fifo.i_available -= i_bits;
232 * Philosophy of the second implementation : WORD_LENGTH (usually 32) bits
233 * are read at the same time, thus minimizing the number of p_byte changes.
234 * Bits are read via GetBits() or ShowBits. This is slightly faster. Be
235 * aware that if, in the forthcoming functions, i_bits > 24, the data have to
236 * be already aligned on an 8-bit boundary, or wrong results will be
240 #if (WORD_TYPE != u32)
241 # error Not supported word
245 * This is stolen from the livid source who stole it from the kernel
246 * FIXME: The macro swab32 for little endian machines does
247 * not seem to work correctly
250 #if defined(SYS_BEOS)
251 # define swab32(x) B_BENDIAN_TO_HOST_INT32(x)
253 # ifdef WORDS_BIG_ENDIAN
254 # define swab32(x) (x)
256 # if defined (HAVE_X86_BSWAP)
257 static __inline__ const u32 __i386_swab32( u32 x )
259 __asm__("bswap %0" : "=r" (x) : "0" (x));
262 # define swab32(x) __i386_swab32(x)
265 ( ( (u32)(((u8*)&x)[0]) << 24 ) | ( (u32)(((u8*)&x)[1]) << 16 ) |\
266 ( (u32)(((u8*)&x)[2]) << 8 ) | ( (u32)(((u8*)&x)[3])) )
271 /*****************************************************************************
272 * ShowBits : return i_bits bits from the bit stream
273 *****************************************************************************/
274 static __inline__ WORD_TYPE ShowWord( bit_stream_t * p_bit_stream )
276 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
278 return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
281 p_bit_stream->pf_next_data_packet( p_bit_stream );
282 return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
285 static __inline__ WORD_TYPE ShowBits( bit_stream_t * p_bit_stream, int i_bits )
287 if( p_bit_stream->fifo.i_available >= i_bits )
289 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
292 return( (p_bit_stream->fifo.buffer |
293 (ShowWord( p_bit_stream ) >> p_bit_stream->fifo.i_available))
294 >> (8 * sizeof(WORD_TYPE) - i_bits) );
297 /*****************************************************************************
298 * GetWord : returns the next word to be read
299 *****************************************************************************/
300 static __inline__ WORD_TYPE GetWord( bit_stream_t * p_bit_stream )
302 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
304 return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
308 p_bit_stream->pf_next_data_packet( p_bit_stream );
309 return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
313 /*****************************************************************************
314 * RemoveBits : removes i_bits bits from the bit buffer
315 *****************************************************************************/
316 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream, int i_bits )
318 p_bit_stream->fifo.i_available -= i_bits;
320 if( p_bit_stream->fifo.i_available >= 0 )
322 p_bit_stream->fifo.buffer <<= i_bits;
325 p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
326 << ( -p_bit_stream->fifo.i_available );
327 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
330 /*****************************************************************************
331 * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
332 * refill it). This should be faster than RemoveBits, though
333 * RemoveBits will work, too.
334 *****************************************************************************/
335 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
337 p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
338 << (32 - p_bit_stream->fifo.i_available);
341 /*****************************************************************************
342 * GetBits : returns i_bits bits from the bit stream and removes them
343 *****************************************************************************/
344 static __inline__ WORD_TYPE GetBits( bit_stream_t * p_bit_stream, int i_bits )
348 p_bit_stream->fifo.i_available -= i_bits;
349 if( p_bit_stream->fifo.i_available >= 0 )
351 i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
352 p_bit_stream->fifo.buffer <<= i_bits;
356 i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
357 p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
358 i_result |= p_bit_stream->fifo.buffer
359 >> (8 * sizeof(WORD_TYPE)
360 + p_bit_stream->fifo.i_available);
361 p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
362 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
367 /*****************************************************************************
368 * GetBits32 : returns 32 bits from the bit stream and removes them
369 *****************************************************************************/
370 static __inline__ WORD_TYPE GetBits32( bit_stream_t * p_bit_stream )
374 i_result = p_bit_stream->fifo.buffer;
375 p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
376 i_result |= p_bit_stream->fifo.buffer
377 >> (p_bit_stream->fifo.i_available);
378 p_bit_stream->fifo.buffer <<= (8 * sizeof(WORD_TYPE)
379 - p_bit_stream->fifo.i_available);
384 /*****************************************************************************
385 * RealignBits : realigns the bit buffer on an 8-bit boundary
386 *****************************************************************************/
387 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
389 p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
390 p_bit_stream->fifo.i_available &= ~0x7;
395 * Philosophy of the third implementation : the decoder asks for n bytes,
396 * and we will copy them in its buffer.
399 /*****************************************************************************
400 * GetChunk : reads a large chunk of data
401 *****************************************************************************
402 * The position in the stream must be byte-aligned, if unsure call
403 * RealignBits(). p_buffer must to a buffer at least as big as i_buf_len
404 * otherwise your code will crash.
405 *****************************************************************************/
406 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
407 byte_t * p_buffer, size_t i_buf_len )
409 ptrdiff_t i_available;
411 if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
414 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
415 p_bit_stream->p_byte += i_buf_len;
421 memcpy( p_buffer, p_bit_stream->p_byte, i_available );
422 p_bit_stream->p_byte = p_bit_stream->p_end;
423 p_buffer += i_available;
424 i_buf_len -= i_available;
425 p_bit_stream->pf_next_data_packet( p_bit_stream );
427 while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
432 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
433 p_bit_stream->p_byte += i_buf_len;
440 * Communication interface between input and decoders
443 /*****************************************************************************
445 *****************************************************************************
446 * Standard pointers given to the decoders as a toolbox.
447 *****************************************************************************/
448 typedef struct decoder_config_s
451 u8 i_type; /* type of the elementary stream */
453 struct stream_ctrl_s * p_stream_ctrl;
454 struct decoder_fifo_s * p_decoder_fifo;
455 void (* pf_init_bit_stream)( struct bit_stream_s *,
456 struct decoder_fifo_s * );
459 /*****************************************************************************
461 *****************************************************************************
462 * Pointers given to video decoders threads.
463 *****************************************************************************/
464 struct vout_thread_s;
466 typedef struct vdec_config_s
468 struct vout_thread_s * p_vout;
470 struct picture_s * (* pf_create_picture)( struct vout_thread_s *,
471 int i_type, int i_width,
473 void (* pf_destroy_picture)( struct vout_thread_s *,
474 struct picture_s * );
475 void (* pf_display_picture)( struct vout_thread_s *,
476 struct picture_s * );
477 void (* pf_date_picture)( struct vout_thread_s *,
478 struct picture_s *, mtime_t date );
479 void (* pf_link_picture)( struct vout_thread_s *,
480 struct picture_s *, mtime_t date );
481 void (* pf_unlink_picture)( struct vout_thread_s *,
482 struct picture_s *, mtime_t date );
483 struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
484 int i_type, int i_size );
485 void (* pf_destroy_subpicture)( struct vout_thread_s *,
486 struct subpicture_s * );
487 void (* pf_display_subpicture)( struct vout_thread_s *,
488 struct subpicture_s * );
490 decoder_config_t decoder_config;
493 /*****************************************************************************
495 *****************************************************************************
496 * Pointers given to audio decoders threads.
497 *****************************************************************************/
498 struct aout_thread_s;
500 typedef struct adec_config_s
502 struct aout_thread_s * p_aout;
504 struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
505 struct aout_fifo_s * );
506 void (* pf_destroy_fifo)( struct aout_thread_s *);
508 decoder_config_t decoder_config;
513 * Communication interface between decoders and input
516 /*****************************************************************************
517 * decoder_capabilities_t
518 *****************************************************************************
519 * Structure returned by a call to GetCapabilities() of the decoder.
520 *****************************************************************************/
521 typedef struct decoder_capabilities_s
524 u8 i_stream_type; /* == i_type in es_descriptor_t */
525 int i_weight; /* for a given stream type, the decoder
526 * with higher weight will be spawned */
528 vlc_thread_t (* pf_create_thread)( void * );
529 } decoder_capabilities_t;