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.27 2001/04/05 16:37:15 asmax 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 */
37 /* Decoders information */
38 byte_t * p_payload_start;
39 /* start of the PES payload in this packet */
40 byte_t * p_payload_end; /* guess ? :-) */
41 boolean_t b_discard_payload; /* is the packet messed up ? */
45 /* Used to chain the TS packets that carry data for a same PES or PSI */
46 struct data_packet_s * p_next;
49 /*****************************************************************************
51 *****************************************************************************
52 * Describes an PES packet, with its properties, and pointers to the TS packets
54 *****************************************************************************/
55 typedef struct pes_packet_s
58 boolean_t b_data_alignment; /* used to find the beginning of
59 * a video or audio unit */
60 boolean_t b_discontinuity; /* This packet doesn't follow the
63 mtime_t i_pts; /* PTS for this packet (zero if unset) */
64 mtime_t i_dts; /* DTS for this packet (zero if unset) */
65 int i_rate; /* current pace of reading
66 * (see stream_control.h) */
68 int i_pes_size; /* size of the current PES packet */
70 /* Pointers to packets (packets are then linked by the p_prev and
71 p_next fields of the data_packet_t struct) */
72 data_packet_t * p_first; /* The first packet contained by this
73 * PES (used by decoders). */
76 /*****************************************************************************
78 *****************************************************************************
79 * This rotative FIFO contains PES packets that are to be decoded.
80 *****************************************************************************/
81 typedef struct decoder_fifo_s
83 /* Thread structures */
84 vlc_mutex_t data_lock; /* fifo data lock */
85 vlc_cond_t data_wait; /* fifo data conditional variable */
88 pes_packet_t * buffer[FIFO_SIZE + 1];
92 /* Communication interface between input and decoders */
93 boolean_t b_die; /* the decoder should return now */
94 boolean_t b_error; /* the decoder is in an error loop */
95 void * p_packets_mgt; /* packets management services
96 * data (netlist...) */
97 void (* pf_delete_pes)( void *, pes_packet_t * );
98 /* function to use when releasing a PES */
101 /* Macros to manage a decoder_fifo_t structure. Please remember to take
102 * data_lock before using them. */
103 #define DECODER_FIFO_ISEMPTY( fifo ) ( (fifo).i_start == (fifo).i_end )
104 #define DECODER_FIFO_ISFULL( fifo ) ( ( ((fifo).i_end + 1 - (fifo).i_start)\
106 #define DECODER_FIFO_START( fifo ) ( (fifo).buffer[ (fifo).i_start ] )
107 #define DECODER_FIFO_INCSTART( fifo ) ( (fifo).i_start = ((fifo).i_start + 1)\
109 #define DECODER_FIFO_END( fifo ) ( (fifo).buffer[ (fifo).i_end ] )
110 #define DECODER_FIFO_INCEND( fifo ) ( (fifo).i_end = ((fifo).i_end + 1) \
113 /*****************************************************************************
114 * bit_fifo_t : bit fifo descriptor
115 *****************************************************************************
116 * This type describes a bit fifo used to store bits while working with the
117 * input stream at the bit level.
118 *****************************************************************************/
119 typedef u32 WORD_TYPE;
121 typedef struct bit_fifo_s
123 /* This unsigned integer allows us to work at the bit level. This buffer
124 * can contain 32 bits, and the used space can be found on the MSb's side
125 * and the available space on the LSb's side. */
128 /* Number of bits available in the bit buffer */
133 /*****************************************************************************
134 * bit_stream_t : bit stream descriptor
135 *****************************************************************************
136 * This type, based on a PES stream, includes all the structures needed to
137 * handle the input stream like a bit stream.
138 *****************************************************************************/
139 typedef struct bit_stream_s
149 /* The decoder fifo contains the data of the PES stream */
150 decoder_fifo_t * p_decoder_fifo;
152 /* Function to jump to the next data packet */
153 void (* pf_next_data_packet)( struct bit_stream_s * );
155 /* Callback to the decoder used when changing data packets ; set
156 * to NULL if your decoder doesn't need it. */
157 void (* pf_bitstream_callback)( struct bit_stream_s *,
158 boolean_t b_new_pes );
159 /* Optional argument to the callback */
160 void * p_callback_arg;
165 /* Current data packet (in the current PES packet of the PES stream) */
166 data_packet_t * p_data;
167 /* Pointer to the next byte that is to be read (in the current packet) */
169 /* Pointer to the last byte that is to be read (in the current packet */
171 /* Temporary buffer in case we're not aligned when changing data packets. */
172 WORD_TYPE i_showbits_buffer;
173 data_packet_t showbits_data;
176 /*****************************************************************************
177 * Inline functions used by the decoders to read bit_stream_t
178 *****************************************************************************/
181 * DISCUSSION : How to use the bit_stream structures
183 * sizeof(WORD_TYPE) (usually 32) bits are read at the same time, thus
184 * minimizing the number of p_byte changes.
185 * Bits are read via GetBits() or ShowBits.
187 * XXX : Be aware that if, in the forthcoming functions, i_bits > 24,
188 * the data have to be already aligned on an 8-bit boundary, or wrong
189 * results will be returned. Use RealignBits() if unsure.
192 #if (WORD_TYPE == u32)
193 # define WORD_AT U32_AT
194 #elif (WORD_TYPE == u64)
195 # define WORD_AT U64_AT
197 # error Unsupported WORD_TYPE
200 /*****************************************************************************
201 * Protoypes from input_ext-dec.c
202 *****************************************************************************/
203 u32 UnalignedShowBits( struct bit_stream_s *, unsigned int );
204 void UnalignedRemoveBits( struct bit_stream_s * );
205 u32 UnalignedGetBits( struct bit_stream_s *, unsigned int );
207 /*****************************************************************************
208 * AlignWord : fill in the bit buffer so that the byte pointer be aligned
209 * on a word boundary (XXX: there must be at least sizeof(WORD_TYPE) - 1
210 * empty bytes in the bit buffer)
211 *****************************************************************************/
212 static __inline__ void AlignWord( bit_stream_t * p_bit_stream )
214 while( (ptrdiff_t)p_bit_stream->p_byte
215 & (sizeof(WORD_TYPE) - 1) )
217 if( p_bit_stream->p_byte < p_bit_stream->p_end )
219 p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
220 << (8 * sizeof(WORD_TYPE) - 8
221 - p_bit_stream->fifo.i_available);
222 p_bit_stream->fifo.i_available += 8;
226 p_bit_stream->pf_next_data_packet( p_bit_stream );
227 p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
228 << (8 * sizeof(WORD_TYPE) - 8
229 - p_bit_stream->fifo.i_available);
230 p_bit_stream->fifo.i_available += 8;
235 /*****************************************************************************
236 * ShowBits : return i_bits bits from the bit stream
237 *****************************************************************************/
238 static __inline__ u32 ShowBits( bit_stream_t * p_bit_stream,
239 unsigned int i_bits )
241 if( p_bit_stream->fifo.i_available >= i_bits )
243 return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
246 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
248 return( (p_bit_stream->fifo.buffer |
249 (WORD_AT( p_bit_stream->p_byte )
250 >> p_bit_stream->fifo.i_available))
251 >> (8 * sizeof(WORD_TYPE) - i_bits) );
254 return UnalignedShowBits( p_bit_stream, i_bits );
257 /*****************************************************************************
258 * RemoveBits : removes i_bits bits from the bit buffer
259 * XXX: do not use for 32 bits, see RemoveBits32
260 *****************************************************************************/
261 static __inline__ void RemoveBits( bit_stream_t * p_bit_stream,
262 unsigned int i_bits )
264 p_bit_stream->fifo.i_available -= i_bits;
266 if( p_bit_stream->fifo.i_available >= 0 )
268 p_bit_stream->fifo.buffer <<= i_bits;
272 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
274 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
275 << ( -p_bit_stream->fifo.i_available );
276 ((WORD_TYPE *)p_bit_stream->p_byte)++;
277 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
281 UnalignedRemoveBits( p_bit_stream );
284 /*****************************************************************************
285 * RemoveBits32 : removes 32 bits from the bit buffer (and as a side effect,
287 *****************************************************************************/
288 #if (WORD_TYPE == u32)
289 static __inline__ void RemoveBits32( bit_stream_t * p_bit_stream )
291 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
293 if( p_bit_stream->fifo.i_available )
295 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte )
296 << (32 - p_bit_stream->fifo.i_available);
297 ((WORD_TYPE *)p_bit_stream->p_byte)++;
301 ((WORD_TYPE *)p_bit_stream->p_byte)++;
305 p_bit_stream->fifo.i_available -= 32;
306 UnalignedRemoveBits( p_bit_stream );
309 # define RemoveBits32( p_bit_stream ) RemoveBits( p_bit_stream, 32 )
312 /*****************************************************************************
313 * GetBits : returns i_bits bits from the bit stream and removes them
314 * XXX: do not use for 32 bits, see GetBits32
315 *****************************************************************************/
316 static __inline__ u32 GetBits( bit_stream_t * p_bit_stream,
317 unsigned int i_bits )
321 p_bit_stream->fifo.i_available -= i_bits;
323 if( p_bit_stream->fifo.i_available >= 0 )
325 i_result = p_bit_stream->fifo.buffer
326 >> (8 * sizeof(WORD_TYPE) - i_bits);
327 p_bit_stream->fifo.buffer <<= i_bits;
331 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
333 i_result = p_bit_stream->fifo.buffer
334 >> (8 * sizeof(WORD_TYPE) - i_bits);
335 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
336 ((WORD_TYPE *)p_bit_stream->p_byte)++;
337 i_result |= p_bit_stream->fifo.buffer
338 >> (8 * sizeof(WORD_TYPE)
339 + p_bit_stream->fifo.i_available);
340 p_bit_stream->fifo.buffer <<= ( -p_bit_stream->fifo.i_available );
341 p_bit_stream->fifo.i_available += sizeof(WORD_TYPE) * 8;
345 return UnalignedGetBits( p_bit_stream, i_bits );
348 /*****************************************************************************
349 * GetBits32 : returns 32 bits from the bit stream and removes them
350 *****************************************************************************/
351 #if (WORD_TYPE == u32)
352 static __inline__ u32 GetBits32( bit_stream_t * p_bit_stream )
356 if( p_bit_stream->fifo.i_available == 32 )
358 p_bit_stream->fifo.i_available = 0;
359 i_result = p_bit_stream->fifo.buffer;
360 p_bit_stream->fifo.buffer = 0;
364 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
366 if( p_bit_stream->fifo.i_available )
368 i_result = p_bit_stream->fifo.buffer;
369 p_bit_stream->fifo.buffer = WORD_AT( p_bit_stream->p_byte );
370 ((WORD_TYPE *)p_bit_stream->p_byte)++;
371 i_result |= p_bit_stream->fifo.buffer
372 >> (p_bit_stream->fifo.i_available);
373 p_bit_stream->fifo.buffer <<= (32 - p_bit_stream->fifo.i_available);
377 i_result = WORD_AT( p_bit_stream->p_byte );
378 ((WORD_TYPE *)p_bit_stream->p_byte)++;
382 p_bit_stream->fifo.i_available -= 32;
383 return UnalignedGetBits( p_bit_stream, 32 );
386 # define GetBits32( p_bit_stream ) GetBits( p_bit_stream, 32 )
389 /*****************************************************************************
390 * RealignBits : realigns the bit buffer on an 8-bit boundary
391 *****************************************************************************/
392 static __inline__ void RealignBits( bit_stream_t * p_bit_stream )
394 p_bit_stream->fifo.buffer <<= (p_bit_stream->fifo.i_available & 0x7);
395 p_bit_stream->fifo.i_available &= ~0x7;
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( p_bit_stream->fifo.i_available )
413 *((WORD_TYPE *)p_buffer) = WORD_AT( &p_bit_stream->fifo.buffer );
414 p_buffer += p_bit_stream->fifo.i_available >> 3;
415 i_buf_len -= p_bit_stream->fifo.i_available >> 3;
416 p_bit_stream->fifo.buffer = 0;
417 p_bit_stream->fifo.i_available = 0;
420 if( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
423 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
424 p_bit_stream->p_byte += i_buf_len;
430 memcpy( p_buffer, p_bit_stream->p_byte, i_available );
431 p_bit_stream->p_byte = p_bit_stream->p_end;
432 p_buffer += i_available;
433 i_buf_len -= i_available;
434 p_bit_stream->pf_next_data_packet( p_bit_stream );
436 while( (i_available = p_bit_stream->p_end - p_bit_stream->p_byte)
437 <= i_buf_len && !p_bit_stream->p_decoder_fifo->b_die );
441 memcpy( p_buffer, p_bit_stream->p_byte, i_buf_len );
442 p_bit_stream->p_byte += i_buf_len;
446 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
448 AlignWord( p_bit_stream );
454 * The following functions are now deprecated.
457 static __inline__ byte_t _GetByte( bit_stream_t * p_bit_stream )
459 if ( p_bit_stream->p_byte >= p_bit_stream->p_end )
461 p_bit_stream->pf_next_data_packet( p_bit_stream );
464 return( *(p_bit_stream->p_byte++) );
467 static __inline__ void NeedBits( bit_stream_t * p_bit_stream, int i_bits )
469 while ( p_bit_stream->fifo.i_available < i_bits )
471 p_bit_stream->fifo.buffer |= ((WORD_TYPE)_GetByte( p_bit_stream ))
472 << (8 * sizeof(WORD_TYPE) - 8
473 - p_bit_stream->fifo.i_available);
474 p_bit_stream->fifo.i_available += 8;
478 static __inline__ void DumpBits( bit_stream_t * p_bit_stream, int i_bits )
480 p_bit_stream->fifo.buffer <<= i_bits;
481 p_bit_stream->fifo.i_available -= i_bits;
486 * Communication interface between input and decoders
489 /*****************************************************************************
491 *****************************************************************************
492 * Standard pointers given to the decoders as a toolbox.
493 *****************************************************************************/
494 typedef struct decoder_config_s
497 u8 i_type; /* type of the elementary stream */
499 struct stream_ctrl_s * p_stream_ctrl;
500 struct decoder_fifo_s * p_decoder_fifo;
501 void (* pf_init_bit_stream)( struct bit_stream_s *,
502 struct decoder_fifo_s * );
505 /*****************************************************************************
507 *****************************************************************************
508 * Pointers given to video decoders threads.
509 *****************************************************************************/
510 struct vout_thread_s;
512 typedef struct vdec_config_s
514 struct vout_thread_s * p_vout;
516 struct picture_s * (* pf_create_picture)( struct vout_thread_s *,
517 int i_type, int i_width,
519 void (* pf_destroy_picture)( struct vout_thread_s *,
520 struct picture_s * );
521 void (* pf_display_picture)( struct vout_thread_s *,
522 struct picture_s * );
523 void (* pf_date_picture)( struct vout_thread_s *,
524 struct picture_s *, mtime_t date );
525 void (* pf_link_picture)( struct vout_thread_s *,
526 struct picture_s *, mtime_t date );
527 void (* pf_unlink_picture)( struct vout_thread_s *,
528 struct picture_s *, mtime_t date );
529 struct subpicture_s *(* pf_create_subpicture)( struct vout_thread_s *,
530 int i_type, int i_size );
531 void (* pf_destroy_subpicture)( struct vout_thread_s *,
532 struct subpicture_s * );
533 void (* pf_display_subpicture)( struct vout_thread_s *,
534 struct subpicture_s * );
536 decoder_config_t decoder_config;
539 /*****************************************************************************
541 *****************************************************************************
542 * Pointers given to audio decoders threads.
543 *****************************************************************************/
544 struct aout_thread_s;
546 typedef struct adec_config_s
548 struct aout_thread_s * p_aout;
550 struct aout_fifo_s * (* pf_create_fifo)( struct aout_thread_s *,
551 struct aout_fifo_s * );
552 void (* pf_destroy_fifo)( struct aout_thread_s *);
554 decoder_config_t decoder_config;
559 * Communication interface between decoders and input
562 /*****************************************************************************
563 * decoder_capabilities_t
564 *****************************************************************************
565 * Structure returned by a call to GetCapabilities() of the decoder.
566 *****************************************************************************/
567 typedef struct decoder_capabilities_s
570 u8 i_stream_type; /* == i_type in es_descriptor_t */
571 int i_weight; /* for a given stream type, the decoder
572 * with higher weight will be spawned */
574 vlc_thread_t (* pf_create_thread)( void * );
575 } decoder_capabilities_t;