1 /*****************************************************************************
2 * input_ext-dec.c: services to the decoders
3 *****************************************************************************
4 * Copyright (C) 1998, 1999, 2000 VideoLAN
5 * $Id: input_ext-dec.c,v 1.18 2001/08/22 17:21:45 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 */
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-plugins.h"
44 /*****************************************************************************
45 * InitBitstream: initialize a bit_stream_t structure
46 *****************************************************************************/
47 void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo,
48 void (* pf_bitstream_callback)( struct bit_stream_s *,
50 void * p_callback_arg )
52 p_bit_stream->p_decoder_fifo = p_fifo;
53 p_bit_stream->pf_next_data_packet = NextDataPacket;
54 p_bit_stream->pf_bitstream_callback = pf_bitstream_callback;
55 p_bit_stream->p_callback_arg = p_callback_arg;
57 /* Get the first data packet. */
58 vlc_mutex_lock( &p_fifo->data_lock );
59 while ( DECODER_FIFO_ISEMPTY( *p_fifo ) )
63 vlc_mutex_unlock( &p_fifo->data_lock );
66 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
68 p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
69 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
70 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
71 p_bit_stream->fifo.buffer = 0;
72 p_bit_stream->fifo.i_available = 0;
73 vlc_mutex_unlock( &p_fifo->data_lock );
75 /* Call back the decoder. */
76 if( p_bit_stream->pf_bitstream_callback != NULL )
78 p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
81 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
83 /* Get aligned on a word boundary.
84 * NB : we _will_ get aligned, because we have at most
85 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
86 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
87 AlignWord( p_bit_stream );
91 /*****************************************************************************
92 * NextDataPacket: go to the next data packet
93 *****************************************************************************/
94 void NextDataPacket( bit_stream_t * p_bit_stream )
96 decoder_fifo_t * p_fifo = p_bit_stream->p_decoder_fifo;
99 /* We are looking for the next data packet that contains real data,
100 * and not just a PES header */
103 /* We were reading the last data packet of this PES packet... It's
104 * time to jump to the next PES packet */
105 if( p_bit_stream->p_data->p_next == NULL )
107 /* We are going to read/write the start and end indexes of the
108 * decoder fifo and to use the fifo's conditional variable,
109 * that's why we need to take the lock before. */
110 vlc_mutex_lock( &p_fifo->data_lock );
112 /* Free the previous PES packet. */
113 p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
114 DECODER_FIFO_START( *p_fifo ) );
115 DECODER_FIFO_INCSTART( *p_fifo );
117 if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
119 /* Wait for the input to tell us when we receive a packet. */
120 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
123 /* The next byte could be found in the next PES packet */
124 p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
126 vlc_mutex_unlock( &p_fifo->data_lock );
132 /* Perhaps the next data packet of the current PES packet contains
133 * real data (ie its payload's size is greater than 0). */
134 p_bit_stream->p_data = p_bit_stream->p_data->p_next;
138 } while ( p_bit_stream->p_data->p_payload_start
139 == p_bit_stream->p_data->p_payload_end );
141 /* We've found a data packet which contains interesting data... */
142 p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
143 p_bit_stream->p_end = p_bit_stream->p_data->p_payload_end;
145 /* Call back the decoder. */
146 if( p_bit_stream->pf_bitstream_callback != NULL )
148 p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
152 /*****************************************************************************
153 * UnalignedShowBits : places i_bits bits into the bit buffer, even when
154 * not aligned on a word boundary
155 *****************************************************************************/
156 void UnalignedShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
158 /* We just fill in the bit buffer. */
159 while( p_bit_stream->fifo.i_available < i_bits )
161 if( p_bit_stream->p_byte < p_bit_stream->p_end )
163 p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
164 << (8 * sizeof(WORD_TYPE) - 8
165 - p_bit_stream->fifo.i_available);
166 p_bit_stream->fifo.i_available += 8;
170 p_bit_stream->pf_next_data_packet( p_bit_stream );
172 if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
174 /* We are not aligned anymore. */
175 if( ((ptrdiff_t)p_bit_stream->p_byte
176 & (sizeof(WORD_TYPE) - 1)) * 8
177 < p_bit_stream->fifo.i_available )
179 /* We are not aligned, and won't be. Copy the first word
180 * of the packet in a temporary buffer, and we'll see
184 /* sizeof(WORD_TYPE) - number of bytes to trash
185 * from the last payload */
188 p_bit_stream->i_showbits_buffer = 0;
190 for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
192 if( p_bit_stream->p_byte >= p_bit_stream->p_end )
195 p_bit_stream->pf_next_data_packet( p_bit_stream );
197 ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
198 * p_bit_stream->p_byte;
199 p_bit_stream->p_byte++;
202 /* This is kind of kludgy. */
203 p_bit_stream->p_data->p_payload_start +=
204 sizeof(WORD_TYPE) - j;
205 p_bit_stream->p_byte =
206 (byte_t *)&p_bit_stream->i_showbits_buffer;
207 p_bit_stream->p_end =
208 (byte_t *)&p_bit_stream->i_showbits_buffer
210 p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
211 p_bit_stream->p_data = &p_bit_stream->showbits_data;
215 /* We are not aligned, but we can be. */
216 AlignWord( p_bit_stream );
223 /*****************************************************************************
224 * UnalignedGetBits : returns i_bits bits from the bit stream and removes
225 * them from the buffer, even when the bit stream is not aligned on a word
227 *****************************************************************************/
228 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
232 i_result = p_bit_stream->fifo.buffer
233 >> (8 * sizeof(WORD_TYPE) - i_bits);
234 i_bits = -p_bit_stream->fifo.i_available;
236 /* Gather missing bytes. */
239 if( p_bit_stream->p_byte < p_bit_stream->p_end )
241 i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
246 p_bit_stream->pf_next_data_packet( p_bit_stream );
247 i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
252 /* Gather missing bits. */
255 unsigned int i_tmp = 8 - i_bits;
257 if( p_bit_stream->p_byte < p_bit_stream->p_end )
259 i_result |= *p_bit_stream->p_byte >> i_tmp;
260 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
261 << ( sizeof(WORD_TYPE) * 8 - i_tmp );
262 p_bit_stream->fifo.i_available = i_tmp;
266 p_bit_stream->pf_next_data_packet( p_bit_stream );
267 i_result |= *p_bit_stream->p_byte >> i_tmp;
268 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
269 << ( sizeof(WORD_TYPE) * 8 - i_tmp );
270 p_bit_stream->fifo.i_available = i_tmp;
275 p_bit_stream->fifo.i_available = 0;
276 p_bit_stream->fifo.buffer = 0;
279 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
281 /* Get aligned on a word boundary. Otherwise it is safer
282 * to do it the next time.
283 * NB : we _will_ get aligned, because we have at most
284 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
285 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
286 AlignWord( p_bit_stream );
292 /*****************************************************************************
293 * UnalignedRemoveBits : removes i_bits (== -i_available) from the bit
294 * buffer, even when the bit stream is not aligned on a word boundary
295 *****************************************************************************/
296 void UnalignedRemoveBits( bit_stream_t * p_bit_stream )
298 /* First remove all unnecessary bytes. */
299 while( p_bit_stream->fifo.i_available <= -8 )
301 if( p_bit_stream->p_byte < p_bit_stream->p_end )
303 p_bit_stream->p_byte++;
304 p_bit_stream->fifo.i_available += 8;
308 p_bit_stream->pf_next_data_packet( p_bit_stream );
309 p_bit_stream->p_byte++;
310 p_bit_stream->fifo.i_available += 8;
314 /* Remove unnecessary bits. */
315 if( p_bit_stream->fifo.i_available < 0 )
317 if( p_bit_stream->p_byte < p_bit_stream->p_end )
319 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
320 << ( sizeof(WORD_TYPE) * 8 - 8
321 - p_bit_stream->fifo.i_available );
322 p_bit_stream->fifo.i_available += 8;
326 p_bit_stream->pf_next_data_packet( p_bit_stream );
327 p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
328 << ( sizeof(WORD_TYPE) * 8 - 8
329 - p_bit_stream->fifo.i_available );
330 p_bit_stream->fifo.i_available += 8;
335 p_bit_stream->fifo.buffer = 0;
338 if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
340 /* Get aligned on a word boundary. Otherwise it is safer
341 * to do it the next time.
342 * NB : we _will_ get aligned, because we have at most
343 * sizeof(WORD_TYPE) - 1 bytes to store, and at least
344 * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
345 AlignWord( p_bit_stream );