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 boolean_t b_error; /* the decoder is in an error loop */
69 void * p_packets_mgt; /* packets management services
70 * data (netlist...) */
71 void (* pf_delete_pes)( void *, pes_packet_t * );
72 /* function to use when releasing a PES */
75 /* Macros to manage a decoder_fifo_t structure. Please remember to take
76 * data_lock before using them. */
77 #define DECODER_FIFO_ISEMPTY( fifo ) ( (fifo).i_start == (fifo).i_end )
78 #define DECODER_FIFO_ISFULL( fifo ) ( ( ((fifo).i_end + 1 - (fifo).i_start)\
80 #define DECODER_FIFO_START( fifo ) ( (fifo).buffer[ (fifo).i_start ] )
81 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
83 #define DECODER_FIFO_END( fifo ) ( (fifo).buffer[ (fifo).i_end ] )
84 #define DECODER_FIFO_INCEND( fifo ) ( (fifo).i_end = ((fifo).i_end + 1) \
87 /*****************************************************************************
88 * bit_fifo_t : bit fifo descriptor
89 *****************************************************************************
90 * This type describes a bit fifo used to store bits while working with the
91 * input stream at the bit level.
92 *****************************************************************************/
93 typedef u32 WORD_TYPE; /* only u32 is supported at the moment */
95 typedef struct bit_fifo_s
97 /* This unsigned integer allows us to work at the bit level. This buffer
98 * can contain 32 bits, and the used space can be found on the MSb's side
99 * and the available space on the LSb's side. */
102 /* Number of bits available in the bit buffer */
107 /*****************************************************************************
108 * bit_stream_t : bit stream descriptor
109 *****************************************************************************
110 * This type, based on a PES stream, includes all the structures needed to
111 * handle the input stream like a bit stream.
112 *****************************************************************************/
113 typedef struct bit_stream_s
118 /* The decoder fifo contains the data of the PES stream */
119 decoder_fifo_t * p_decoder_fifo;
121 /* Function to jump to the next data packet */
122 void (* pf_next_data_packet)( struct bit_stream_s * );
127 /* Current data packet (in the current PES packet of the PES stream) */
128 data_packet_t * p_data;
129 /* Pointer to the next byte that is to be read (in the current TS packet) */
131 /* Pointer to the last byte that is to be read (in the current TS packet */
140 /*****************************************************************************
141 * Inline functions used by the decoders to read bit_stream_t
142 *****************************************************************************/
145 * Philosophy of the first implementation : the bit buffer is first filled by
146 * NeedBits, then the buffer can be read via p_bit_stream->fifo.buffer, and
147 * unnecessary bits are dumped with a DumpBits() call.
150 /*****************************************************************************
151 * GetByte : reads the next byte in the input stream
152 *****************************************************************************/
153 static __inline__ byte_t GetByte( bit_stream_t * p_bit_stream )
155 /* Are there some bytes left in the current data packet ? */
156 /* could change this test to have a if (! (bytes--)) instead */
157 if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
159 /* no, switch to next data packet */
160 p_bit_stream->pf_next_data_packet( p_bit_stream );
163 return( *(p_bit_stream->p_byte++) );
166 /*****************************************************************************
167 * NeedBits : reads i_bits new bits in the bit stream and stores them in the
169 *****************************************************************************
170 * - i_bits must be less or equal 32 !
171 * - There is something important to notice with that function : if the number
172 * of bits available in the bit buffer when calling NeedBits() is greater than
173 * 24 (i_available > 24) but less than the number of needed bits
174 * (i_available < i_bits), the byte returned by GetByte() will be shifted with
175 * a negative value and the number of bits available in the bit buffer will be
176 * set to more than 32 !
177 *****************************************************************************/
178 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
180 while ( p_bit_stream->fifo.i_available < i_bits )
182 p_bit_stream->fifo.buffer |= ((WORD_TYPE)GetByte( p_bit_stream ))
183 << (sizeof(WORD_TYPE) - 8
184 - p_bit_stream->fifo.i_available);
185 p_bit_stream->fifo.i_available += 8;
189 /*****************************************************************************
190 * DumpBits : removes i_bits bits from the bit buffer
191 *****************************************************************************
192 * - i_bits <= i_available
193 * - i_bits < 32 (because (u32 << 32) <=> (u32 = u32))
194 *****************************************************************************/
195 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
197 p_bit_stream->fifo.buffer <<= i_bits;
198 p_bit_stream->fifo.i_available -= i_bits;
203 * Philosophy of the second implementation : WORD_LENGTH (usually 32) bits
204 * are read at the same time, thus minimizing the number of p_byte changes.
205 * Bits are read via GetBits() or ShowBits. This is slightly faster. Be
206 * aware that if, in the forthcoming functions, i_bits > 24, the data have to
207 * be already aligned on an 8-bit boundary, or wrong results will be
211 #if (WORD_TYPE != u32)
212 # error Not supported word
216 * This is stolen from the livid source who stole it from the kernel
217 * FIXME: The macro swab32 for little endian machines does
218 * not seem to work correctly
221 #if defined(SYS_BEOS)
222 # define swab32(x) B_BENDIAN_TO_HOST_INT32(x)
224 # if __BYTE_ORDER == __BIG_ENDIAN
225 # define swab32(x) (x)
227 # if defined (__i386__)
228 static __inline__ const u32 __i386_swab32( u32 x )
230 __asm__("bswap %0" : "=r" (x) : "0" (x));
233 # define swab32(x) __i386_swab32(x)
236 ( ( (u32)(((u8*)&x)[0]) << 24 ) | ( (u32)(((u8*)&x)[1]) << 16 ) |\
237 ( (u32)(((u8*)&x)[2]) << 8 ) | ( (u32)(((u8*)&x)[3])) )
242 /*****************************************************************************
243 * ShowBits : return i_bits bits from the bit stream
244 *****************************************************************************/
245 static __inline__ WORD_TYPE ShowWord( bit_stream_t * p_bit_stream )
247 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
249 return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
252 p_bit_stream->pf_next_data_packet( p_bit_stream );
253 return( swab32( *((WORD_TYPE *)p_bit_stream->p_byte) ) );
256 static __inline__ WORD_TYPE ShowBits( bit_stream_t * p_bit_stream, int i_bits )
258 if( p_bit_stream->fifo.i_available >= i_bits )
260 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
263 return( (p_bit_stream->fifo.buffer |
264 (ShowWord( p_bit_stream ) >> p_bit_stream->fifo.i_available))
265 >> (8 * sizeof(WORD_TYPE) - i_bits) );
268 /*****************************************************************************
269 * GetWord : returns the next word to be read
270 *****************************************************************************/
271 static __inline__ WORD_TYPE GetWord( bit_stream_t * p_bit_stream )
273 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
275 return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
279 p_bit_stream->pf_next_data_packet( p_bit_stream );
280 return( swab32( *(((WORD_TYPE *)p_bit_stream->p_byte)++) ) );
284 /*****************************************************************************
285 * RemoveBits : removes i_bits bits from the bit buffer
286 *****************************************************************************/
287 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream, int i_bits )
289 p_bit_stream->fifo.i_available -= i_bits;
291 if( p_bit_stream->fifo.i_available >= 0 )
293 p_bit_stream->fifo.buffer <<= i_bits;
296 p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
297 << ( -p_bit_stream->fifo.i_available );
298 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
301 /*****************************************************************************
302 * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
303 * refill it). This should be faster than RemoveBits, though
304 * RemoveBits will work, too.
305 *****************************************************************************/
306 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
308 p_bit_stream->fifo.buffer = GetWord( p_bit_stream )
309 << (32 - p_bit_stream->fifo.i_available);
312 /*****************************************************************************
313 * GetBits : returns i_bits bits from the bit stream and removes them
314 *****************************************************************************/
315 static __inline__ WORD_TYPE GetBits( bit_stream_t * p_bit_stream, int i_bits )
319 p_bit_stream->fifo.i_available -= i_bits;
320 if( p_bit_stream->fifo.i_available >= 0 )
322 i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
323 p_bit_stream->fifo.buffer <<= i_bits;
327 i_result = p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits);
328 p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
329 i_result |= p_bit_stream->fifo.buffer
330 >> (8 * sizeof(WORD_TYPE)
331 + p_bit_stream->fifo.i_available);
332 p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
333 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
338 /*****************************************************************************
339 * GetBits32 : returns 32 bits from the bit stream and removes them
340 *****************************************************************************/
341 static __inline__ WORD_TYPE GetBits32( bit_stream_t * p_bit_stream )
345 i_result = p_bit_stream->fifo.buffer;
346 p_bit_stream->fifo.buffer = GetWord( p_bit_stream );
347 i_result |= p_bit_stream->fifo.buffer
348 >> (p_bit_stream->fifo.i_available);
349 p_bit_stream->fifo.buffer <<= (8 * sizeof(WORD_TYPE)
350 - p_bit_stream->fifo.i_available);
355 /*****************************************************************************
356 * RealignBits : realigns the bit buffer on an 8-bit boundary
357 *****************************************************************************/
358 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
360 p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
361 p_bit_stream->fifo.i_available &= ~0x7;
366 * Philosophy of the third implementation : the decoder asks for n bytes,
367 * and we will copy them in its buffer.
370 /*****************************************************************************
371 * GetChunk : reads a large chunk of data
372 *****************************************************************************
373 * The position in the stream must be byte-aligned, if unsure call
374 * RealignBits(). p_buffer must to a buffer at least as big as i_buf_len
375 * otherwise your code will crash.
376 *****************************************************************************/
377 static __inline__ void GetChunk( bit_stream_t * p_bit_stream,
378 byte_t * p_buffer, size_t i_buf_len )
382 if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
385 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
386 p_bit_stream->p_byte += i_buf_len;
392 memcpy( p_buffer, p_bit_stream->p_byte, i_available );
393 p_bit_stream->p_byte = p_bit_stream->p_end;
394 p_buffer += i_available;
395 i_buf_len -= i_available;
396 p_bit_stream->pf_next_data_packet( p_bit_stream );
398 while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
403 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
404 p_bit_stream->p_byte += i_buf_len;
411 * Communication interface between input and decoders
414 /*****************************************************************************
416 *****************************************************************************
417 * Standard pointers given to the decoders as a toolbox.
418 *****************************************************************************/
419 typedef struct decoder_config_s
422 u8 i_type; /* type of the elementary stream */
424 struct stream_ctrl_s * p_stream_ctrl;
425 struct decoder_fifo_s * p_decoder_fifo;
426 void (* pf_init_bit_stream)( struct bit_stream_s *,
427 struct decoder_fifo_s * );
430 /*****************************************************************************
432 *****************************************************************************
433 * Pointers given to video decoders threads.
434 *****************************************************************************/
435 struct vout_thread_s;
437 typedef struct vdec_config_s
439 struct vout_thread_s * p_vout;
441 struct picture_s * (* pf_create_picture)( struct vout_thread_s *,
442 int i_type, int i_width,
444 void (* pf_destroy_picture)( struct vout_thread_s *,
445 struct picture_s * );
446 void (* pf_display_picture)( struct vout_thread_s *,
447 struct picture_s * );
448 void (* pf_date_picture)( struct vout_thread_s *,
449 struct picture_s *, mtime_t date );
450 void (* pf_link_picture)( struct vout_thread_s *,
451 struct picture_s *, mtime_t date );
452 void (* pf_unlink_picture)( struct vout_thread_s *,
453 struct picture_s *, mtime_t date );
454 struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
455 int i_type, int i_size );
456 void (* pf_destroy_subpicture)( struct vout_thread_s *,
457 struct subpicture_s * );
458 void (* pf_display_subpicture)( struct vout_thread_s *,
459 struct subpicture_s * );
461 decoder_config_t decoder_config;
464 /*****************************************************************************
466 *****************************************************************************
467 * Pointers given to audio decoders threads.
468 *****************************************************************************/
469 struct aout_thread_s;
471 typedef struct adec_config_s
473 struct aout_thread_s * p_aout;
475 struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
476 struct aout_fifo_s * );
477 void (* pf_destroy_fifo)( struct aout_thread_s *);
479 decoder_config_t decoder_config;
484 * Communication interface between decoders and input
487 /*****************************************************************************
488 * decoder_capabilities_t
489 *****************************************************************************
490 * Structure returned by a call to GetCapabilities() of the decoder.
491 *****************************************************************************/
492 typedef struct decoder_capabilities_s
495 u8 i_stream_type; /* == i_type in es_descriptor_t */
496 int i_weight; /* for a given stream type, the decoder
497 * with higher weight will be spawned */
499 vlc_thread_t (* pf_create_thread)( struct decoder_config_s * );
500 } decoder_capabilities_t;