]> git.sesse.net Git - vlc/blob - src/input/input_ext-dec.c
Some heavy changes today:
[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.26 2001/12/30 07:09:56 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 <string.h>                                    /* memcpy(), memset() */
28 #include <sys/types.h>                                              /* off_t */
29
30 #include <videolan/vlc.h>
31
32 #include "stream_control.h"
33 #include "input_ext-dec.h"
34 #include "input_ext-intf.h"
35 #include "input_ext-plugins.h"
36
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 *,
42                                                     boolean_t ),
43                     void * p_callback_arg )
44 {
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;
49
50     /* Get the first data packet. */
51     vlc_mutex_lock( &p_fifo->data_lock );
52     while ( p_fifo->p_first == NULL )
53     {
54         if ( p_fifo->b_die )
55         {
56             vlc_mutex_unlock( &p_fifo->data_lock );
57             return;
58         }
59         vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
60     }
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 );
67
68     /* Call back the decoder. */
69     if( p_bit_stream->pf_bitstream_callback != NULL )
70     {
71         p_bit_stream->pf_bitstream_callback( p_bit_stream, 1 );
72     }
73
74     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
75     {
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 );
81     }
82 }
83
84 /*****************************************************************************
85  * DecoderError : an error occured, use this function to empty the fifo
86  *****************************************************************************/
87 void DecoderError( decoder_fifo_t * p_fifo )
88 {
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);
92
93     /* Wait until a `die' order is sent */
94     while (!p_fifo->b_die)
95     {
96         /* Trash all received PES packets */
97         p_fifo->pf_delete_pes( p_fifo->p_packets_mgt, p_fifo->p_first );
98
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);
101     }
102
103     /* We can release the lock before leaving */
104     vlc_mutex_unlock (&p_fifo->data_lock);
105 }
106
107 /*****************************************************************************
108  * NextDataPacket: go to the next data packet
109  *****************************************************************************/
110 void NextDataPacket( bit_stream_t * p_bit_stream )
111 {
112     decoder_fifo_t *    p_fifo = p_bit_stream->p_decoder_fifo;
113     boolean_t           b_new_pes;
114
115     /* We are looking for the next data packet that contains real data,
116      * and not just a PES header */
117     do
118     {
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 )
122         {
123             pes_packet_t * p_next;
124
125             vlc_mutex_lock( &p_fifo->data_lock );
126
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,
131                                    p_fifo->p_first );
132             p_fifo->p_first = p_next;
133             p_fifo->i_depth--;
134
135             if( p_fifo->p_first == NULL )
136             {
137                 /* No PES in the FIFO. p_last is no longer valid. */
138                 p_fifo->pp_last = &p_fifo->p_first;
139
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 );
142             }
143
144             /* The next byte could be found in the next PES packet */
145             p_bit_stream->p_data = p_fifo->p_first->p_first;
146
147             vlc_mutex_unlock( &p_fifo->data_lock );
148
149             b_new_pes = 1;
150         }
151         else
152         {
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;
156
157             b_new_pes = 0;
158         }
159     } while ( p_bit_stream->p_data->p_payload_start
160                == p_bit_stream->p_data->p_payload_end );
161
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;
165
166     /* Call back the decoder. */
167     if( p_bit_stream->pf_bitstream_callback != NULL )
168     {
169         p_bit_stream->pf_bitstream_callback( p_bit_stream, b_new_pes );
170     }
171 }
172
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 )
178 {
179     /* We just fill in the bit buffer. */
180     while( p_bit_stream->fifo.i_available < i_bits )
181     {
182         if( p_bit_stream->p_byte < p_bit_stream->p_end )
183         {
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;
188         }
189         else
190         {
191             p_bit_stream->pf_next_data_packet( p_bit_stream );
192
193             if( (ptrdiff_t)p_bit_stream->p_byte & (sizeof(WORD_TYPE) - 1) )
194             {
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 )
199                 {
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
202                      * later. */
203                     int     i;
204
205                     /* sizeof(WORD_TYPE) - number of bytes to trash
206                      * from the last payload */
207                     int     j;
208
209                     p_bit_stream->i_showbits_buffer = 0;
210
211                     for( j = i = 0 ; i < sizeof(WORD_TYPE) ; i++ )
212                     {
213                         if( p_bit_stream->p_byte >= p_bit_stream->p_end )
214                         {
215                             j = i;
216                             p_bit_stream->pf_next_data_packet( p_bit_stream );
217                         }
218                         ((byte_t *)&p_bit_stream->i_showbits_buffer)[i] =
219                             * p_bit_stream->p_byte;
220                         p_bit_stream->p_byte++;
221                     }
222
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
230                             + sizeof(WORD_TYPE);
231                     p_bit_stream->showbits_data.p_next = p_bit_stream->p_data;
232                     p_bit_stream->p_data = &p_bit_stream->showbits_data;
233                 }
234                 else
235                 {
236                     /* We are not aligned, but we can be. */
237                     AlignWord( p_bit_stream );
238                 }
239             }
240
241             /* We have 32 bits ready for reading, it will be enough. */
242             break;
243         }
244     }
245
246     /* It shouldn't loop :-)) */
247     return( ShowBits( p_bit_stream, i_bits ) );
248 }
249
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
253  * boundary
254  *****************************************************************************/
255 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
256 {
257     u32         i_result;
258
259     i_result = p_bit_stream->fifo.buffer
260                     >> (8 * sizeof(WORD_TYPE) - i_bits);
261     i_bits = -p_bit_stream->fifo.i_available;
262
263     /* Gather missing bytes. */
264     while( i_bits >= 8 )
265     {
266         if( p_bit_stream->p_byte < p_bit_stream->p_end )
267         {
268             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
269             i_bits -= 8;
270         }
271         else
272         {
273             p_bit_stream->pf_next_data_packet( p_bit_stream );
274             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
275             i_bits -= 8;
276         }
277     }
278
279     /* Gather missing bits. */
280     if( i_bits > 0 )
281     {
282         unsigned int    i_tmp = 8 - i_bits;
283
284         if( p_bit_stream->p_byte < p_bit_stream->p_end )
285         {
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;
290         }
291         else
292         {
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;
298         }
299     }
300     else
301     {
302         p_bit_stream->fifo.i_available = 0;
303         p_bit_stream->fifo.buffer = 0;
304     }
305
306     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
307     {
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 );
314     }
315
316     return( i_result );
317 }
318
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 )
324 {
325     /* First remove all unnecessary bytes. */
326     while( p_bit_stream->fifo.i_available <= -8 )
327     {
328         if( p_bit_stream->p_byte < p_bit_stream->p_end )
329         {
330             p_bit_stream->p_byte++;
331             p_bit_stream->fifo.i_available += 8;
332         }
333         else
334         {
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;
338         }
339     }
340
341     /* Remove unnecessary bits. */
342     if( p_bit_stream->fifo.i_available < 0 )
343     {
344         if( p_bit_stream->p_byte < p_bit_stream->p_end )
345         {
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;
350         }
351         else
352         {
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;
358         }
359     }
360     else
361     {
362         p_bit_stream->fifo.buffer = 0;
363     }
364
365     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
366     {
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 );
373     }
374 }
375