]> git.sesse.net Git - vlc/blob - src/input/input_ext-dec.c
7c88025fe1cc8d033e311ce9a1ab217c89c37f44
[vlc] / src / input / input_ext-dec.c
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.17 2001/07/17 09:48:08 massiot Exp $
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include "defs.h"
28
29 #include <string.h>                                    /* memcpy(), memset() */
30 #include <sys/types.h>                                              /* off_t */
31
32 #include "config.h"
33 #include "common.h"
34 #include "threads.h"
35 #include "mtime.h"
36
37 #include "intf_msg.h"
38
39 #include "stream_control.h"
40 #include "input_ext-dec.h"
41 #include "input_ext-intf.h"
42 #include "input_ext-plugins.h"
43
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 *,
49                                                     boolean_t ),
50                     void * p_callback_arg )
51 {
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;
56
57     /* Get the first data packet. */
58     vlc_mutex_lock( &p_fifo->data_lock );
59     while ( DECODER_FIFO_ISEMPTY( *p_fifo ) )
60     {
61         if ( p_fifo->b_die )
62         {
63             vlc_mutex_unlock( &p_fifo->data_lock );
64             return;
65         }
66         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
67     }
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 );
74
75     /* Call back the decoder. */
76     if( p_bit_stream->pf_bitstream_callback != NULL )
77     {
78         p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
79     }
80
81     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
82     {
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 );
88     }
89 }
90
91 /*****************************************************************************
92  * NextDataPacket: go to the next data packet
93  *****************************************************************************/
94 void NextDataPacket( bit_stream_t * p_bit_stream )
95 {
96     decoder_fifo_t *    p_fifo = p_bit_stream->p_decoder_fifo;
97     boolean_t           b_new_pes;
98
99     /* We are looking for the next data packet that contains real data,
100      * and not just a PES header */
101     do
102     {
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 )
106         {
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 );
111
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 );
116
117             if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
118             {
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 );
121             }
122
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;
125
126             vlc_mutex_unlock( &p_fifo->data_lock );
127
128             b_new_pes = 1;
129         }
130         else
131         {
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;
135
136             b_new_pes = 0;
137         }
138     } while ( p_bit_stream->p_data->p_payload_start
139                == p_bit_stream->p_data->p_payload_end );
140
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;
144
145     /* Call back the decoder. */
146     if( p_bit_stream->pf_bitstream_callback != NULL )
147     {
148         p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
149     }
150 }
151
152 /*****************************************************************************
153  * UnalignedShowBits : return i_bits bits from the bit stream, even when
154  * not aligned on a word boundary
155  *****************************************************************************/
156 u32 UnalignedShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
157 {
158     /* We just fill in the bit buffer. */
159     while( p_bit_stream->fifo.i_available < i_bits )
160     {
161         if( p_bit_stream->p_byte < p_bit_stream->p_end )
162         {
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;
167         }
168         else
169         {
170             p_bit_stream->pf_next_data_packet( p_bit_stream );
171
172             if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
173             {
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 )
178                 {
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
181                      * later. */
182                     int     i;
183
184                     /* sizeof(WORD_TYPE) - number of bytes to trash
185                      * from the last payload */
186                     int     j;
187
188                     p_bit_stream->i_showbits_buffer = 0;
189
190                     for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
191                     {
192                         if( p_bit_stream->p_byte >= p_bit_stream->p_end )
193                         {
194                             j = i;
195                             p_bit_stream->pf_next_data_packet( p_bit_stream );
196                         }
197                         ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
198                             * p_bit_stream->p_byte;
199                         p_bit_stream->p_byte++;
200                     }
201
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
209                             + sizeof(WORD_TYPE);
210                     p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
211                     p_bit_stream->p_data = &p_bit_stream->showbits_data;
212                 }
213                 else
214                 {
215                     /* We are not aligned, but we can be. */
216                     AlignWord( p_bit_stream );
217                 }
218             }
219
220             return( ShowBits( p_bit_stream, i_bits ) );
221         }
222     }
223     return( p_bit_stream->fifo.buffer >> (8 * sizeof(WORD_TYPE) - i_bits) );
224 }
225
226 /*****************************************************************************
227  * UnalignedGetBits : returns i_bits bits from the bit stream and removes
228  * them from the buffer, even when the bit stream is not aligned on a word
229  * boundary
230  *****************************************************************************/
231 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
232 {
233     u32         i_result;
234
235     i_result = p_bit_stream->fifo.buffer
236                     >> (8 * sizeof(WORD_TYPE) - i_bits);
237     i_bits = -p_bit_stream->fifo.i_available;
238
239     /* Gather missing bytes. */
240     while( i_bits >= 8 )
241     {
242         if( p_bit_stream->p_byte < p_bit_stream->p_end )
243         {
244             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
245             i_bits -= 8;
246         }
247         else
248         {
249             p_bit_stream->pf_next_data_packet( p_bit_stream );
250             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
251             i_bits -= 8;
252         }
253     }
254
255     /* Gather missing bits. */
256     if( i_bits > 0 )
257     {
258         unsigned int    i_tmp = 8 - i_bits;
259
260         if( p_bit_stream->p_byte < p_bit_stream->p_end )
261         {
262             i_result |= *p_bit_stream->p_byte >> i_tmp;
263             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
264                  << ( sizeof(WORD_TYPE) * 8 - i_tmp );
265             p_bit_stream->fifo.i_available = i_tmp;
266         }
267         else
268         {
269             p_bit_stream->pf_next_data_packet( p_bit_stream );
270             i_result |= *p_bit_stream->p_byte >> i_tmp;
271             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
272                  << ( sizeof(WORD_TYPE) * 8 - i_tmp );
273             p_bit_stream->fifo.i_available = i_tmp;
274         }
275     }
276     else
277     {
278         p_bit_stream->fifo.i_available = 0;
279         p_bit_stream->fifo.buffer = 0;
280     }
281
282     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
283     {
284         /* Get aligned on a word boundary. Otherwise it is safer
285          * to do it the next time.
286          * NB : we _will_ get aligned, because we have at most 
287          * sizeof(WORD_TYPE) - 1 bytes to store, and at least
288          * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
289         AlignWord( p_bit_stream );
290     }
291
292     return( i_result );
293 }
294
295 /*****************************************************************************
296  * UnalignedRemoveBits : removes i_bits (== -i_available) from the bit
297  * buffer, even when the bit stream is not aligned on a word boundary
298  *****************************************************************************/
299 void UnalignedRemoveBits( bit_stream_t * p_bit_stream )
300 {
301     /* First remove all unnecessary bytes. */
302     while( p_bit_stream->fifo.i_available <= -8 )
303     {
304         if( p_bit_stream->p_byte < p_bit_stream->p_end )
305         {
306             p_bit_stream->p_byte++;
307             p_bit_stream->fifo.i_available += 8;
308         }
309         else
310         {
311             p_bit_stream->pf_next_data_packet( p_bit_stream );
312             p_bit_stream->p_byte++;
313             p_bit_stream->fifo.i_available += 8;
314         }
315     }
316
317     /* Remove unnecessary bits. */
318     if( p_bit_stream->fifo.i_available < 0 )
319     {
320         if( p_bit_stream->p_byte < p_bit_stream->p_end )
321         {
322             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
323                  << ( sizeof(WORD_TYPE) * 8 - 8
324                          - p_bit_stream->fifo.i_available );
325             p_bit_stream->fifo.i_available += 8;
326         }
327         else
328         {
329             p_bit_stream->pf_next_data_packet( p_bit_stream );
330             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
331                  << ( sizeof(WORD_TYPE) * 8 - 8
332                          - p_bit_stream->fifo.i_available );
333             p_bit_stream->fifo.i_available += 8;
334         }
335     }
336     else
337     {
338         p_bit_stream->fifo.buffer = 0;
339     }
340
341     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
342     {
343         /* Get aligned on a word boundary. Otherwise it is safer
344          * to do it the next time.
345          * NB : we _will_ get aligned, because we have at most 
346          * sizeof(WORD_TYPE) - 1 bytes to store, and at least
347          * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
348         AlignWord( p_bit_stream );
349     }
350 }
351