1 /*****************************************************************************
2 * input_ext-dec.c: services to the decoders
3 *****************************************************************************
4 * Copyright (C) 1998-2001 VideoLAN
5 * $Id: input_ext-dec.c,v 1.26 2001/12/30 07:09:56 sam Exp $
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
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 /*****************************************************************************
26 *****************************************************************************/
27 #include <string.h> /* memcpy(), memset() */
28 #include <sys/types.h> /* off_t */
30 #include <videolan/vlc.h>
32 #include "stream_control.h"
33 #include "input_ext-dec.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-plugins.h"
37 /*****************************************************************************
38 * InitBitstream: initialize a bit_stream_t structure
39 *****************************************************************************/
40 void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo,
41 void (* pf_bitstream_callback)( struct bit_stream_s *,
43 void * p_callback_arg )
45 p_bit_stream->p_decoder_fifo = p_fifo;
46 p_bit_stream->pf_next_data_packet = NextDataPacket;
47 p_bit_stream->pf_bitstream_callback = pf_bitstream_callback;
48 p_bit_stream->p_callback_arg = p_callback_arg;
50 /* Get the first data packet. */
51 vlc_mutex_lock( &p_fifo->data_lock );
52 while ( p_fifo->p_first == NULL )
56 vlc_mutex_unlock( &p_fifo->data_lock );
59 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
61 p_bit_stream->p_data = p_fifo->p_first->p_first;
62 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
63 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
64 p_bit_stream->fifo.buffer = 0;
65 p_bit_stream->fifo.i_available = 0;
66 vlc_mutex_unlock( &p_fifo->data_lock );
68 /* Call back the decoder. */
69 if( p_bit_stream->pf_bitstream_callback != NULL )
71 p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
74 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
76 /* Get aligned on a word boundary.
77 * NB : we _will_ get aligned, because we have at most
78 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
79 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
80 AlignWord( p_bit_stream );
84 /*****************************************************************************
85 * DecoderError : an error occured, use this function to empty the fifo
86 *****************************************************************************/
87 void DecoderError( decoder_fifo_t * p_fifo )
89 /* We take the lock, because we are going to read/write the start/end
90 * indexes of the decoder fifo */
91 vlc_mutex_lock (&p_fifo->data_lock);
93 /* Wait until a `die' order is sent */
94 while (!p_fifo->b_die)
96 /* Trash all received PES packets */
97 p_fifo->pf_delete_pes( p_fifo->p_packets_mgt, p_fifo->p_first );
99 /* Waiting for the input thread to put new PES packets in the fifo */
100 vlc_cond_wait (&p_fifo->data_wait, &p_fifo->data_lock);
103 /* We can release the lock before leaving */
104 vlc_mutex_unlock (&p_fifo->data_lock);
107 /*****************************************************************************
108 * NextDataPacket: go to the next data packet
109 *****************************************************************************/
110 void NextDataPacket( bit_stream_t * p_bit_stream )
112 decoder_fifo_t * p_fifo = p_bit_stream->p_decoder_fifo;
115 /* We are looking for the next data packet that contains real data,
116 * and not just a PES header */
119 /* We were reading the last data packet of this PES packet... It's
120 * time to jump to the next PES packet */
121 if( p_bit_stream->p_data->p_next == NULL )
123 pes_packet_t * p_next;
125 vlc_mutex_lock( &p_fifo->data_lock );
127 /* Free the previous PES packet. */
128 p_next = p_fifo->p_first->p_next;
129 p_fifo->p_first->p_next = NULL;
130 p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
132 p_fifo->p_first = p_next;
135 if( p_fifo->p_first == NULL )
137 /* No PES in the FIFO. p_last is no longer valid. */
138 p_fifo->pp_last = &p_fifo->p_first;
140 /* Wait for the input to tell us when we receive a packet. */
141 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
144 /* The next byte could be found in the next PES packet */
145 p_bit_stream->p_data = p_fifo->p_first->p_first;
147 vlc_mutex_unlock( &p_fifo->data_lock );
153 /* Perhaps the next data packet of the current PES packet contains
154 * real data (ie its payload's size is greater than 0). */
155 p_bit_stream->p_data = p_bit_stream->p_data->p_next;
159 } while ( p_bit_stream->p_data->p_payload_start
160 == p_bit_stream->p_data->p_payload_end );
162 /* We've found a data packet which contains interesting data... */
163 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
164 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
166 /* Call back the decoder. */
167 if( p_bit_stream->pf_bitstream_callback != NULL )
169 p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
173 /*****************************************************************************
174 * UnalignedShowBits : places i_bits bits into the bit buffer, even when
175 * not aligned on a word boundary
176 *****************************************************************************/
177 u32 UnalignedShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
179 /* We just fill in the bit buffer. */
180 while( p_bit_stream->fifo.i_available < i_bits )
182 if( p_bit_stream->p_byte < p_bit_stream->p_end )
184 p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
185 << (8 * sizeof(WORD_TYPE) - 8
186 - p_bit_stream->fifo.i_available);
187 p_bit_stream->fifo.i_available += 8;
191 p_bit_stream->pf_next_data_packet( p_bit_stream );
193 if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
195 /* We are not aligned anymore. */
196 if( ((ptrdiff_t)p_bit_stream->p_byte
197 & (sizeof(WORD_TYPE) - 1)) * 8
198 < p_bit_stream->fifo.i_available )
200 /* We are not aligned, and won't be. Copy the first word
201 * of the packet in a temporary buffer, and we'll see
205 /* sizeof(WORD_TYPE) - number of bytes to trash
206 * from the last payload */
209 p_bit_stream->i_showbits_buffer = 0;
211 for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
213 if( p_bit_stream->p_byte >= p_bit_stream->p_end )
216 p_bit_stream->pf_next_data_packet( p_bit_stream );
218 ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
219 * p_bit_stream->p_byte;
220 p_bit_stream->p_byte++;
223 /* This is kind of kludgy. */
224 p_bit_stream->p_data->p_payload_start +=
225 sizeof(WORD_TYPE) - j;
226 p_bit_stream->p_byte =
227 (byte_t *)&p_bit_stream->i_showbits_buffer;
228 p_bit_stream->p_end =
229 (byte_t *)&p_bit_stream->i_showbits_buffer
231 p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
232 p_bit_stream->p_data = &p_bit_stream->showbits_data;
236 /* We are not aligned, but we can be. */
237 AlignWord( p_bit_stream );
241 /* We have 32 bits ready for reading, it will be enough. */
246 /* It shouldn't loop :-)) */
247 return( ShowBits( p_bit_stream, i_bits ) );
250 /*****************************************************************************
251 * UnalignedGetBits : returns i_bits bits from the bit stream and removes
252 * them from the buffer, even when the bit stream is not aligned on a word
254 *****************************************************************************/
255 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
259 i_result = p_bit_stream->fifo.buffer
260 >> (8 * sizeof(WORD_TYPE) - i_bits);
261 i_bits = -p_bit_stream->fifo.i_available;
263 /* Gather missing bytes. */
266 if( p_bit_stream->p_byte < p_bit_stream->p_end )
268 i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
273 p_bit_stream->pf_next_data_packet( p_bit_stream );
274 i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
279 /* Gather missing bits. */
282 unsigned int i_tmp = 8 - i_bits;
284 if( p_bit_stream->p_byte < p_bit_stream->p_end )
286 i_result |= *p_bit_stream->p_byte >> i_tmp;
287 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
288 << ( sizeof(WORD_TYPE) * 8 - i_tmp );
289 p_bit_stream->fifo.i_available = i_tmp;
293 p_bit_stream->pf_next_data_packet( p_bit_stream );
294 i_result |= *p_bit_stream->p_byte >> i_tmp;
295 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
296 << ( sizeof(WORD_TYPE) * 8 - i_tmp );
297 p_bit_stream->fifo.i_available = i_tmp;
302 p_bit_stream->fifo.i_available = 0;
303 p_bit_stream->fifo.buffer = 0;
306 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
308 /* Get aligned on a word boundary. Otherwise it is safer
309 * to do it the next time.
310 * NB : we _will_ get aligned, because we have at most
311 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
312 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
313 AlignWord( p_bit_stream );
319 /*****************************************************************************
320 * UnalignedRemoveBits : removes i_bits (== -i_available) from the bit
321 * buffer, even when the bit stream is not aligned on a word boundary
322 *****************************************************************************/
323 void UnalignedRemoveBits( bit_stream_t * p_bit_stream )
325 /* First remove all unnecessary bytes. */
326 while( p_bit_stream->fifo.i_available <= -8 )
328 if( p_bit_stream->p_byte < p_bit_stream->p_end )
330 p_bit_stream->p_byte++;
331 p_bit_stream->fifo.i_available += 8;
335 p_bit_stream->pf_next_data_packet( p_bit_stream );
336 p_bit_stream->p_byte++;
337 p_bit_stream->fifo.i_available += 8;
341 /* Remove unnecessary bits. */
342 if( p_bit_stream->fifo.i_available < 0 )
344 if( p_bit_stream->p_byte < p_bit_stream->p_end )
346 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
347 << ( sizeof(WORD_TYPE) * 8 - 8
348 - p_bit_stream->fifo.i_available );
349 p_bit_stream->fifo.i_available += 8;
353 p_bit_stream->pf_next_data_packet( p_bit_stream );
354 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
355 << ( sizeof(WORD_TYPE) * 8 - 8
356 - p_bit_stream->fifo.i_available );
357 p_bit_stream->fifo.i_available += 8;
362 p_bit_stream->fifo.buffer = 0;
365 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
367 /* Get aligned on a word boundary. Otherwise it is safer
368 * to do it the next time.
369 * NB : we _will_ get aligned, because we have at most
370 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
371 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
372 AlignWord( p_bit_stream );