1 /* Structures exported to the decoders */
3 /*****************************************************************************
5 *****************************************************************************
6 * Describe a data packet.
7 *****************************************************************************/
8 typedef struct data_packet_s
10 /* Nothing before this line, the code relies on that */
11 byte_t * p_buffer; /* raw data packet */
13 /* Decoders information */
14 byte_t * p_payload_start;
15 /* start of the PES payload in this packet */
16 byte_t * p_payload_end; /* guess ? :-) */
17 boolean_t b_discard_payload; /* is the packet messed up ? */
19 /* Used to chain the TS packets that carry data for a same PES or PSI */
20 struct data_packet_s * p_next;
23 /*****************************************************************************
25 *****************************************************************************
26 * Describes an PES packet, with its properties, and pointers to the TS packets
28 *****************************************************************************/
29 typedef struct pes_packet_s
32 boolean_t b_messed_up; /* At least one of the data packets
33 * has a questionable content */
34 boolean_t b_data_alignment; /* used to find the beginning of
35 * a video or audio unit */
36 boolean_t b_discontinuity; /* This packet doesn't follow the
39 boolean_t b_has_pts; /* is the following field set ? */
40 mtime_t i_pts; /* the PTS for this packet (if set above) */
42 int i_pes_size; /* size of the current PES packet */
44 /* Pointers to packets (packets are then linked by the p_prev and
45 p_next fields of the data_packet_t struct) */
46 data_packet_t * p_first; /* The first packet containing this
47 * PES (used by decoders). */
50 /*****************************************************************************
52 *****************************************************************************
53 * This rotative FIFO contains PES packets that are to be decoded.
54 *****************************************************************************/
55 typedef struct decoder_fifo_s
57 /* Thread structures */
58 vlc_mutex_t data_lock; /* fifo data lock */
59 vlc_cond_t data_wait; /* fifo data conditional variable */
62 pes_packet_t * buffer[FIFO_SIZE + 1];
66 /* Communication interface between input and decoders */
67 boolean_t b_die; /* the decoder should return now */
68 void * p_packets_mgt; /* packets management services
69 * data (netlist...) */
70 void (* pf_delete_pes)( void *, pes_packet_t * );
71 /* function to use when releasing a PES */
74 /* Macros to manage a decoder_fifo_t structure. Please remember to take
75 * data_lock before using them. */
76 #define DECODER_FIFO_ISEMPTY( fifo ) ( (fifo).i_start == (fifo).i_end )
77 #define DECODER_FIFO_ISFULL( fifo ) ( ( ((fifo).i_end + 1 - (fifo).i_start)\
79 #define DECODER_FIFO_START( fifo ) ( (fifo).buffer[ (fifo).i_start ] )
80 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
82 #define DECODER_FIFO_END( fifo ) ( (fifo).buffer[ (fifo).i_end ] )
83 #define DECODER_FIFO_INCEND( fifo ) ( (fifo).i_end = ((fifo).i_end + 1) \
86 /*****************************************************************************
87 * bit_fifo_t : bit fifo descriptor
88 *****************************************************************************
89 * This type describes a bit fifo used to store bits while working with the
90 * input stream at the bit level.
91 *****************************************************************************/
92 typedef u32 WORD_TYPE; /* only u32 is supported at the moment */
94 typedef struct bit_fifo_s
96 /* This unsigned integer allows us to work at the bit level. This buffer
97 * can contain 32 bits, and the used space can be found on the MSb's side
98 * and the available space on the LSb's side. */
101 /* Number of bits available in the bit buffer */
106 /*****************************************************************************
107 * bit_stream_t : bit stream descriptor
108 *****************************************************************************
109 * This type, based on a PES stream, includes all the structures needed to
110 * handle the input stream like a bit stream.
111 *****************************************************************************/
112 typedef struct bit_stream_s
117 /* The decoder fifo contains the data of the PES stream */
118 decoder_fifo_t * p_decoder_fifo;
120 /* Function to jump to the next data packet */
121 void (* pf_next_data_packet)( struct bit_stream_s * );
126 /* Current data packet (in the current PES packet of the PES stream) */
127 data_packet_t * p_data;
128 /* Pointer to the next byte that is to be read (in the current TS packet) */
130 /* Pointer to the last byte that is to be read (in the current TS packet */
139 /*****************************************************************************
140 * Inline functions used by the decoders to read bit_stream_t
141 *****************************************************************************/
144 * Philosophy of the first implementation : the bit buffer is first filled by
145 * NeedBits, then the buffer can be read via p_bit_stream->fifo.buffer, and
146 * unnecessary bits are dumped with a DumpBits() call.
149 /*****************************************************************************
150 * GetByte : reads the next byte in the input stream
151 *****************************************************************************/
152 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
154 /* Are there some bytes left in the current data packet ? */
155 /* could change this test to have a if (! (bytes--)) instead */
156 if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
158 /* no, switch to next data packet */
159 p_bit_stream->pf_next_data_packet( p_bit_stream );
162 return( *(p_bit_stream->p_byte++) );
165 /*****************************************************************************
166 * NeedBits : reads i_bits new bits in the bit stream and stores them in the
168 *****************************************************************************
169 * - i_bits must be less or equal 32 !
170 * - There is something important to notice with that function : if the number
171 * of bits available in the bit buffer when calling NeedBits() is greater than
172 * 24 (i_available > 24) but less than the number of needed bits
173 * (i_available < i_bits), the byte returned by GetByte() will be shifted with
174 * a negative value and the number of bits available in the bit buffer will be
175 * set to more than 32 !
176 *****************************************************************************/
177 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
179 while ( p_bit_stream->fifo.i_available < i_bits )
181 p_bit_stream->fifo.buffer |= ((WORD_TYPE)GetByte( p_bit_stream ))
182 << (sizeof(WORD_TYPE) - 8
183 - p_bit_stream->fifo.i_available);
184 p_bit_stream->fifo.i_available += 8;
188 /*****************************************************************************
189 * DumpBits : removes i_bits bits from the bit buffer
190 *****************************************************************************
191 * - i_bits <= i_available
192 * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
193 *****************************************************************************/
194 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
196 p_bit_stream->fifo.buffer <<= i_bits;
197 p_bit_stream->fifo.i_available -= i_bits;
202 * Philosophy of the second implementation : WORD_LENGTH (usually 32) bits
203 * are read at the same time, thus minimizing the number of p_byte changes.
204 * Bits are read via GetBits() or ShowBits. This is slightly faster. Be
205 * aware that if, in the forthcoming functions, i_bits > 24, the data have to
206 * be already aligned on an 8-bit boundary, or wrong results will be
210 #if (WORD_TYPE != u32)
211 # error Not supported word
215 * This is stolen from the livid source who stole it from the kernel
216 * FIXME: The macro swab32 for little endian machines does
217 * not seem to work correctly
220 #if defined(SYS_BEOS)
221 # define swab32(x) B_BENDIAN_TO_HOST_INT32(x)
223 # if __BYTE_ORDER == __BIG_ENDIAN
224 # define swab32(x) (x)
226 # if defined (__i386__)
227 static __inline__ const u32 __i386_swab32( u32 x )
229 __asm__("bswap %0" : "=r" (x) : "0" (x));
232 # define swab32(x) __i386_swab32(x)
235 ( ( (u32)(((u8*)&x)[0]) << 24 ) | ( (u32)(((u8*)&x)[1]) << 16 ) |\
236 ( (u32)(((u8*)&x)[2]) << 8 ) | ( (u32)(((u8*)&x)[3])) )
241 /*****************************************************************************
242 * ShowBits : return i_bits bits from the bit stream
243 *****************************************************************************/
244 static __inline__ WORD_TYPE ShowWord( bit_stream_t * p_bit_stream )
246 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
248 return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
251 p_bit_stream->pf_next_data_packet( p_bit_stream );
252 return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
255 static __inline__ WORD_TYPE ShowBits( bit_stream_t * p_bit_stream, int i_bits )
257 if( p_bit_stream->fifo.i_available >= i_bits )
259 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
262 return( (p_bit_stream->fifo.buffer |
263 (ShowWord( p_bit_stream ) >> p_bit_stream->fifo.i_available))
264 >> (8 * sizeof(WORD_TYPE) - i_bits) );
267 /*****************************************************************************
268 * GetWord : returns the next word to be read
269 *****************************************************************************/
270 static __inline__ WORD_TYPE GetWord( bit_stream_t * p_bit_stream )
272 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
274 return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
278 p_bit_stream->pf_next_data_packet( p_bit_stream );
279 return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
283 /*****************************************************************************
284 * RemoveBits : removes i_bits bits from the bit buffer
285 *****************************************************************************/
286 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream, int i_bits )
288 p_bit_stream->fifo.i_available -= i_bits;
290 if( p_bit_stream->fifo.i_available >= 0 )
292 p_bit_stream->fifo.buffer <<= i_bits;
295 p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
296 << ( -p_bit_stream->fifo.i_available );
297 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
300 /*****************************************************************************
301 * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
302 * refill it). This should be faster than RemoveBits, though
303 * RemoveBits will work, too.
304 *****************************************************************************/
305 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
307 p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
308 << (32 - p_bit_stream->fifo.i_available);
311 /*****************************************************************************
312 * GetBits : returns i_bits bits from the bit stream and removes them
313 *****************************************************************************/
314 static __inline__ WORD_TYPE GetBits( bit_stream_t * p_bit_stream, int i_bits )
318 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 >> (8 * sizeof(WORD_TYPE) - i_bits);
322 p_bit_stream->fifo.buffer <<= i_bits;
326 i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
327 p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
328 i_result |= p_bit_stream->fifo.buffer
329 >> (8 * sizeof(WORD_TYPE)
330 + p_bit_stream->fifo.i_available);
331 p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
332 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
337 /*****************************************************************************
338 * GetBits32 : returns 32 bits from the bit stream and removes them
339 *****************************************************************************/
340 static __inline__ WORD_TYPE GetBits32( bit_stream_t * p_bit_stream )
344 i_result = p_bit_stream->fifo.buffer;
345 p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
346 i_result |= p_bit_stream->fifo.buffer
347 >> (p_bit_stream->fifo.i_available);
348 p_bit_stream->fifo.buffer <<= (8 * sizeof(WORD_TYPE)
349 - p_bit_stream->fifo.i_available);
354 /*****************************************************************************
355 * RealignBits : realigns the bit buffer on an 8-bit boundary
356 *****************************************************************************/
357 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
359 p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
360 p_bit_stream->fifo.i_available &= ~0x7;
365 * Communication interface between input and decoders
368 /*****************************************************************************
370 *****************************************************************************
371 * Standard pointers given to the decoders as a toolbox.
372 *****************************************************************************/
373 typedef struct decoder_config_s
376 u8 i_type; /* type of the elementary stream */
378 struct stream_ctrl_s * p_stream_ctrl;
379 struct decoder_fifo_s * p_decoder_fifo;
380 void (* pf_init_bit_stream)( struct bit_stream_s *,
381 struct decoder_fifo_s * );
384 /*****************************************************************************
386 *****************************************************************************
387 * Pointers given to video decoders threads.
388 *****************************************************************************/
389 struct vout_thread_s;
391 typedef struct vdec_config_s
393 struct vout_thread_s * p_vout;
395 struct picture_s * (* pf_create_picture)( struct vout_thread_s *,
396 int i_type, int i_width,
398 void (* pf_destroy_picture)( struct vout_thread_s *,
399 struct picture_s * );
400 void (* pf_display_picture)( struct vout_thread_s *,
401 struct picture_s * );
402 void (* pf_date_picture)( struct vout_thread_s *,
403 struct picture_s *, mtime_t date );
404 void (* pf_link_picture)( struct vout_thread_s *,
405 struct picture_s *, mtime_t date );
406 void (* pf_unlink_picture)( struct vout_thread_s *,
407 struct picture_s *, mtime_t date );
408 struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
409 int i_type, int i_size );
410 void (* pf_destroy_subpicture)( struct vout_thread_s *,
411 struct subpicture_s * );
412 void (* pf_display_subpicture)( struct vout_thread_s *,
413 struct subpicture_s * );
415 decoder_config_t decoder_config;
418 /*****************************************************************************
420 *****************************************************************************
421 * Pointers given to audio decoders threads.
422 *****************************************************************************/
423 struct aout_thread_s;
425 typedef struct adec_config_s
427 struct aout_thread_s * p_aout;
429 struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
430 struct aout_fifo_s * );
431 void (* pf_destroy_fifo)( struct aout_thread_s *);
433 decoder_config_t decoder_config;
438 * Communication interface between decoders and input
441 /*****************************************************************************
442 * decoder_capabilities_t
443 *****************************************************************************
444 * Structure returned by a call to GetCapabilities() of the decoder.
445 *****************************************************************************/
446 typedef struct decoder_capabilities_s
449 u8 i_stream_type; /* == i_type in es_descriptor_t */
450 int i_weight; /* for a given stream type, the decoder
451 * with higher weight will be spawned */
453 vlc_thread_t (* pf_create_thread)( struct decoder_config_s * );
454 } decoder_capabilities_t;