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.21 2001/11/28 15:08:06 massiot 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 *****************************************************************************/
29 #include <string.h> /* memcpy(), memset() */
30 #include <sys/types.h> /* off_t */
38 #include "stream_control.h"
39 #include "input_ext-dec.h"
40 #include "input_ext-intf.h"
41 #include "input_ext-plugins.h"
43 /*****************************************************************************
44 * InitBitstream: initialize a bit_stream_t structure
45 *****************************************************************************/
46 void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo,
47 void (* pf_bitstream_callback)( struct bit_stream_s *,
49 void * p_callback_arg )
51 p_bit_stream->p_decoder_fifo = p_fifo;
52 p_bit_stream->pf_next_data_packet = NextDataPacket;
53 p_bit_stream->pf_bitstream_callback = pf_bitstream_callback;
54 p_bit_stream->p_callback_arg = p_callback_arg;
56 /* Get the first data packet. */
57 vlc_mutex_lock( &p_fifo->data_lock );
58 while ( DECODER_FIFO_ISEMPTY( *p_fifo ) )
62 vlc_mutex_unlock( &p_fifo->data_lock );
65 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
67 p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
68 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
69 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
70 p_bit_stream->fifo.buffer = 0;
71 p_bit_stream->fifo.i_available = 0;
72 vlc_mutex_unlock( &p_fifo->data_lock );
74 /* Call back the decoder. */
75 if( p_bit_stream->pf_bitstream_callback != NULL )
77 p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
80 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
82 /* Get aligned on a word boundary.
83 * NB : we _will_ get aligned, because we have at most
84 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
85 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
86 AlignWord( p_bit_stream );
90 /*****************************************************************************
91 * NextDataPacket: go to the next data packet
92 *****************************************************************************/
93 void NextDataPacket( bit_stream_t * p_bit_stream )
95 decoder_fifo_t * p_fifo = p_bit_stream->p_decoder_fifo;
98 /* We are looking for the next data packet that contains real data,
99 * and not just a PES header */
102 /* We were reading the last data packet of this PES packet... It's
103 * time to jump to the next PES packet */
104 if( p_bit_stream->p_data->p_next == NULL )
106 /* We are going to read/write the start and end indexes of the
107 * decoder fifo and to use the fifo's conditional variable,
108 * that's why we need to take the lock before. */
109 vlc_mutex_lock( &p_fifo->data_lock );
111 /* Free the previous PES packet. */
112 p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
113 DECODER_FIFO_START( *p_fifo ) );
114 DECODER_FIFO_INCSTART( *p_fifo );
116 if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
118 /* Wait for the input to tell us when we receive a packet. */
119 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
122 /* The next byte could be found in the next PES packet */
123 p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
125 vlc_mutex_unlock( &p_fifo->data_lock );
131 /* Perhaps the next data packet of the current PES packet contains
132 * real data (ie its payload's size is greater than 0). */
133 p_bit_stream->p_data = p_bit_stream->p_data->p_next;
137 } while ( p_bit_stream->p_data->p_payload_start
138 == p_bit_stream->p_data->p_payload_end );
140 /* We've found a data packet which contains interesting data... */
141 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
142 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
144 /* Call back the decoder. */
145 if( p_bit_stream->pf_bitstream_callback != NULL )
147 p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
151 /*****************************************************************************
152 * UnalignedShowBits : places i_bits bits into the bit buffer, even when
153 * not aligned on a word boundary
154 *****************************************************************************/
155 u32 UnalignedShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
157 /* We just fill in the bit buffer. */
158 while( p_bit_stream->fifo.i_available < i_bits )
160 if( p_bit_stream->p_byte < p_bit_stream->p_end )
162 p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
163 << (8 * sizeof(WORD_TYPE) - 8
164 - p_bit_stream->fifo.i_available);
165 p_bit_stream->fifo.i_available += 8;
169 p_bit_stream->pf_next_data_packet( p_bit_stream );
171 if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
173 /* We are not aligned anymore. */
174 if( ((ptrdiff_t)p_bit_stream->p_byte
175 & (sizeof(WORD_TYPE) - 1)) * 8
176 < p_bit_stream->fifo.i_available )
178 /* We are not aligned, and won't be. Copy the first word
179 * of the packet in a temporary buffer, and we'll see
183 /* sizeof(WORD_TYPE) - number of bytes to trash
184 * from the last payload */
187 p_bit_stream->i_showbits_buffer = 0;
189 for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
191 if( p_bit_stream->p_byte >= p_bit_stream->p_end )
194 p_bit_stream->pf_next_data_packet( p_bit_stream );
196 ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
197 * p_bit_stream->p_byte;
198 p_bit_stream->p_byte++;
201 /* This is kind of kludgy. */
202 p_bit_stream->p_data->p_payload_start +=
203 sizeof(WORD_TYPE) - j;
204 p_bit_stream->p_byte =
205 (byte_t *)&p_bit_stream->i_showbits_buffer;
206 p_bit_stream->p_end =
207 (byte_t *)&p_bit_stream->i_showbits_buffer
209 p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
210 p_bit_stream->p_data = &p_bit_stream->showbits_data;
214 /* We are not aligned, but we can be. */
215 AlignWord( p_bit_stream );
219 /* We have 32 bits ready for reading, it will be enough. */
224 /* It shouldn't loop :-)) */
225 return( ShowBits( p_bit_stream, i_bits ) );
228 /*****************************************************************************
229 * UnalignedGetBits : returns i_bits bits from the bit stream and removes
230 * them from the buffer, even when the bit stream is not aligned on a word
232 *****************************************************************************/
233 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
237 i_result = p_bit_stream->fifo.buffer
238 >> (8 * sizeof(WORD_TYPE) - i_bits);
239 i_bits = -p_bit_stream->fifo.i_available;
241 /* Gather missing bytes. */
244 if( p_bit_stream->p_byte < p_bit_stream->p_end )
246 i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
251 p_bit_stream->pf_next_data_packet( p_bit_stream );
252 i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
257 /* Gather missing bits. */
260 unsigned int i_tmp = 8 - i_bits;
262 if( p_bit_stream->p_byte < p_bit_stream->p_end )
264 i_result |= *p_bit_stream->p_byte >> i_tmp;
265 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
266 << ( sizeof(WORD_TYPE) * 8 - i_tmp );
267 p_bit_stream->fifo.i_available = i_tmp;
271 p_bit_stream->pf_next_data_packet( p_bit_stream );
272 i_result |= *p_bit_stream->p_byte >> i_tmp;
273 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
274 << ( sizeof(WORD_TYPE) * 8 - i_tmp );
275 p_bit_stream->fifo.i_available = i_tmp;
280 p_bit_stream->fifo.i_available = 0;
281 p_bit_stream->fifo.buffer = 0;
284 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
286 /* Get aligned on a word boundary. Otherwise it is safer
287 * to do it the next time.
288 * NB : we _will_ get aligned, because we have at most
289 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
290 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
291 AlignWord( p_bit_stream );
297 /*****************************************************************************
298 * UnalignedRemoveBits : removes i_bits (== -i_available) from the bit
299 * buffer, even when the bit stream is not aligned on a word boundary
300 *****************************************************************************/
301 void UnalignedRemoveBits( bit_stream_t * p_bit_stream )
303 /* First remove all unnecessary bytes. */
304 while( p_bit_stream->fifo.i_available <= -8 )
306 if( p_bit_stream->p_byte < p_bit_stream->p_end )
308 p_bit_stream->p_byte++;
309 p_bit_stream->fifo.i_available += 8;
313 p_bit_stream->pf_next_data_packet( p_bit_stream );
314 p_bit_stream->p_byte++;
315 p_bit_stream->fifo.i_available += 8;
319 /* Remove unnecessary bits. */
320 if( p_bit_stream->fifo.i_available < 0 )
322 if( p_bit_stream->p_byte < p_bit_stream->p_end )
324 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
325 << ( sizeof(WORD_TYPE) * 8 - 8
326 - p_bit_stream->fifo.i_available );
327 p_bit_stream->fifo.i_available += 8;
331 p_bit_stream->pf_next_data_packet( p_bit_stream );
332 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
333 << ( sizeof(WORD_TYPE) * 8 - 8
334 - p_bit_stream->fifo.i_available );
335 p_bit_stream->fifo.i_available += 8;
340 p_bit_stream->fifo.buffer = 0;
343 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
345 /* Get aligned on a word boundary. Otherwise it is safer
346 * to do it the next time.
347 * NB : we _will_ get aligned, because we have at most
348 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
349 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
350 AlignWord( p_bit_stream );