]> git.sesse.net Git - vlc/blob - src/input/input_ext-dec.c
c8a0a127cfb53b4934a60e753708dbe3e4757f06
[vlc] / src / input / input_ext-dec.c
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.22 2001/12/09 17:01:37 sam 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 "common.h"
33 #include "intf_msg.h"
34 #include "threads.h"
35 #include "mtime.h"
36
37 #include "stream_control.h"
38 #include "input_ext-dec.h"
39 #include "input_ext-intf.h"
40 #include "input_ext-plugins.h"
41
42 /*****************************************************************************
43  * InitBitstream: initialize a bit_stream_t structure
44  *****************************************************************************/
45 void InitBitstream( bit_stream_t * p_bit_stream, decoder_fifo_t * p_fifo,
46                     void (* pf_bitstream_callback)( struct bit_stream_s *,
47                                                     boolean_t ),
48                     void * p_callback_arg )
49 {
50     p_bit_stream->p_decoder_fifo = p_fifo;
51     p_bit_stream->pf_next_data_packet = NextDataPacket;
52     p_bit_stream->pf_bitstream_callback = pf_bitstream_callback;
53     p_bit_stream->p_callback_arg = p_callback_arg;
54
55     /* Get the first data packet. */
56     vlc_mutex_lock( &p_fifo->data_lock );
57     while ( DECODER_FIFO_ISEMPTY( *p_fifo ) )
58     {
59         if ( p_fifo->b_die )
60         {
61             vlc_mutex_unlock( &p_fifo->data_lock );
62             return;
63         }
64         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
65     }
66     p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
67     p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
68     p_bit_stream->p_end  = p_bit_stream->p_data->p_payload_end;
69     p_bit_stream->fifo.buffer = 0;
70     p_bit_stream->fifo.i_available = 0;
71     vlc_mutex_unlock( &p_fifo->data_lock );
72
73     /* Call back the decoder. */
74     if( p_bit_stream->pf_bitstream_callback != NULL )
75     {
76         p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
77     }
78
79     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
80     {
81         /* Get aligned on a word boundary.
82          * NB : we _will_ get aligned, because we have at most 
83          * sizeof(WORD_TYPE) - 1 bytes to store, and at least
84          * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
85         AlignWord( p_bit_stream );
86     }
87 }
88
89 /*****************************************************************************
90  * NextDataPacket: go to the next data packet
91  *****************************************************************************/
92 void NextDataPacket( bit_stream_t * p_bit_stream )
93 {
94     decoder_fifo_t *    p_fifo = p_bit_stream->p_decoder_fifo;
95     boolean_t           b_new_pes;
96
97     /* We are looking for the next data packet that contains real data,
98      * and not just a PES header */
99     do
100     {
101         /* We were reading the last data packet of this PES packet... It's
102          * time to jump to the next PES packet */
103         if( p_bit_stream->p_data->p_next == NULL )
104         {
105             /* We are going to read/write the start and end indexes of the
106              * decoder fifo and to use the fifo's conditional variable,
107              * that's why we need to take the lock before. */
108             vlc_mutex_lock( &p_fifo->data_lock );
109
110             /* Free the previous PES packet. */
111             p_fifo->pf_delete_pes( p_fifo->p_packets_mgt,
112                                    DECODER_FIFO_START( *p_fifo ) );
113             DECODER_FIFO_INCSTART( *p_fifo );
114
115             if( DECODER_FIFO_ISEMPTY( *p_fifo ) )
116             {
117                 /* Wait for the input to tell us when we receive a packet. */
118                 vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
119             }
120
121             /* The next byte could be found in the next PES packet */
122             p_bit_stream->p_data = DECODER_FIFO_START( *p_fifo )->p_first;
123
124             vlc_mutex_unlock( &p_fifo->data_lock );
125
126             b_new_pes = 1;
127         }
128         else
129         {
130             /* Perhaps the next data packet of the current PES packet contains
131              * real data (ie its payload's size is greater than 0). */
132             p_bit_stream->p_data = p_bit_stream->p_data->p_next;
133
134             b_new_pes = 0;
135         }
136     } while ( p_bit_stream->p_data->p_payload_start
137                == p_bit_stream->p_data->p_payload_end );
138
139     /* We've found a data packet which contains interesting data... */
140     p_bit_stream->p_byte = p_bit_stream->p_data->p_payload_start;
141     p_bit_stream->p_end  = p_bit_stream->p_data->p_payload_end;
142
143     /* Call back the decoder. */
144     if( p_bit_stream->pf_bitstream_callback != NULL )
145     {
146         p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
147     }
148 }
149
150 /*****************************************************************************
151  * UnalignedShowBits : places i_bits bits into the bit buffer, even when
152  * not aligned on a word boundary
153  *****************************************************************************/
154 u32 UnalignedShowBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
155 {
156     /* We just fill in the bit buffer. */
157     while( p_bit_stream->fifo.i_available < i_bits )
158     {
159         if( p_bit_stream->p_byte < p_bit_stream->p_end )
160         {
161             p_bit_stream->fifo.buffer |= *(p_bit_stream->p_byte++)
162                                             << (8 * sizeof(WORD_TYPE) - 8
163                                             - p_bit_stream->fifo.i_available);
164             p_bit_stream->fifo.i_available += 8;
165         }
166         else
167         {
168             p_bit_stream->pf_next_data_packet( p_bit_stream );
169
170             if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
171             {
172                 /* We are not aligned anymore. */
173                 if( ((ptrdiff_t)p_bit_stream->p_byte
174                                     & (sizeof(WORD_TYPE) - 1)) * 8
175                         < p_bit_stream->fifo.i_available )
176                 {
177                     /* We are not aligned, and won't be. Copy the first word
178                      * of the packet in a temporary buffer, and we'll see
179                      * later. */
180                     int     i;
181
182                     /* sizeof(WORD_TYPE) - number of bytes to trash
183                      * from the last payload */
184                     int     j;
185
186                     p_bit_stream->i_showbits_buffer = 0;
187
188                     for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
189                     {
190                         if( p_bit_stream->p_byte >= p_bit_stream->p_end )
191                         {
192                             j = i;
193                             p_bit_stream->pf_next_data_packet( p_bit_stream );
194                         }
195                         ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
196                             * p_bit_stream->p_byte;
197                         p_bit_stream->p_byte++;
198                     }
199
200                     /* This is kind of kludgy. */
201                     p_bit_stream->p_data->p_payload_start +=
202                                                          sizeof(WORD_TYPE) - j;
203                     p_bit_stream->p_byte =
204                         (byte_t *)&p_bit_stream->i_showbits_buffer;
205                     p_bit_stream->p_end =
206                         (byte_t *)&p_bit_stream->i_showbits_buffer
207                             + sizeof(WORD_TYPE);
208                     p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
209                     p_bit_stream->p_data = &p_bit_stream->showbits_data;
210                 }
211                 else
212                 {
213                     /* We are not aligned, but we can be. */
214                     AlignWord( p_bit_stream );
215                 }
216             }
217
218             /* We have 32 bits ready for reading, it will be enough. */
219             break;
220         }
221     }
222
223     /* It shouldn't loop :-)) */
224     return( ShowBits( p_bit_stream, i_bits ) );
225 }
226
227 /*****************************************************************************
228  * UnalignedGetBits : returns i_bits bits from the bit stream and removes
229  * them from the buffer, even when the bit stream is not aligned on a word
230  * boundary
231  *****************************************************************************/
232 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
233 {
234     u32         i_result;
235
236     i_result = p_bit_stream->fifo.buffer
237                     >> (8 * sizeof(WORD_TYPE) - i_bits);
238     i_bits = -p_bit_stream->fifo.i_available;
239
240     /* Gather missing bytes. */
241     while( i_bits >= 8 )
242     {
243         if( p_bit_stream->p_byte < p_bit_stream->p_end )
244         {
245             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
246             i_bits -= 8;
247         }
248         else
249         {
250             p_bit_stream->pf_next_data_packet( p_bit_stream );
251             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
252             i_bits -= 8;
253         }
254     }
255
256     /* Gather missing bits. */
257     if( i_bits > 0 )
258     {
259         unsigned int    i_tmp = 8 - i_bits;
260
261         if( p_bit_stream->p_byte < p_bit_stream->p_end )
262         {
263             i_result |= *p_bit_stream->p_byte >> i_tmp;
264             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
265                  << ( sizeof(WORD_TYPE) * 8 - i_tmp );
266             p_bit_stream->fifo.i_available = i_tmp;
267         }
268         else
269         {
270             p_bit_stream->pf_next_data_packet( p_bit_stream );
271             i_result |= *p_bit_stream->p_byte >> i_tmp;
272             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
273                  << ( sizeof(WORD_TYPE) * 8 - i_tmp );
274             p_bit_stream->fifo.i_available = i_tmp;
275         }
276     }
277     else
278     {
279         p_bit_stream->fifo.i_available = 0;
280         p_bit_stream->fifo.buffer = 0;
281     }
282
283     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
284     {
285         /* Get aligned on a word boundary. Otherwise it is safer
286          * to do it the next time.
287          * NB : we _will_ get aligned, because we have at most
288          * sizeof(WORD_TYPE) - 1 bytes to store, and at least
289          * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
290         AlignWord( p_bit_stream );
291     }
292
293     return( i_result );
294 }
295
296 /*****************************************************************************
297  * UnalignedRemoveBits : removes i_bits (== -i_available) from the bit
298  * buffer, even when the bit stream is not aligned on a word boundary
299  *****************************************************************************/
300 void UnalignedRemoveBits( bit_stream_t * p_bit_stream )
301 {
302     /* First remove all unnecessary bytes. */
303     while( p_bit_stream->fifo.i_available <= -8 )
304     {
305         if( p_bit_stream->p_byte < p_bit_stream->p_end )
306         {
307             p_bit_stream->p_byte++;
308             p_bit_stream->fifo.i_available += 8;
309         }
310         else
311         {
312             p_bit_stream->pf_next_data_packet( p_bit_stream );
313             p_bit_stream->p_byte++;
314             p_bit_stream->fifo.i_available += 8;
315         }
316     }
317
318     /* Remove unnecessary bits. */
319     if( p_bit_stream->fifo.i_available < 0 )
320     {
321         if( p_bit_stream->p_byte < p_bit_stream->p_end )
322         {
323             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
324                  << ( sizeof(WORD_TYPE) * 8 - 8
325                          - p_bit_stream->fifo.i_available );
326             p_bit_stream->fifo.i_available += 8;
327         }
328         else
329         {
330             p_bit_stream->pf_next_data_packet( p_bit_stream );
331             p_bit_stream->fifo.buffer = *(p_bit_stream->p_byte++)
332                  << ( sizeof(WORD_TYPE) * 8 - 8
333                          - p_bit_stream->fifo.i_available );
334             p_bit_stream->fifo.i_available += 8;
335         }
336     }
337     else
338     {
339         p_bit_stream->fifo.buffer = 0;
340     }
341
342     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
343     {
344         /* Get aligned on a word boundary. Otherwise it is safer
345          * to do it the next time.
346          * NB : we _will_ get aligned, because we have at most 
347          * sizeof(WORD_TYPE) - 1 bytes to store, and at least
348          * sizeof(WORD_TYPE) - 1 empty bytes in the bit buffer. */
349         AlignWord( p_bit_stream );
350     }
351 }
352