]> git.sesse.net Git - vlc/blob - src/input/input_ext-dec.c
* Totally rewrote the video decoder (inspired by walken's mpeg2dec), implying :
[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.18 2001/08/22 17:21:45 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 : 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 )
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     }
221 }
222
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
226  * boundary
227  *****************************************************************************/
228 u32 UnalignedGetBits( bit_stream_t * p_bit_stream, unsigned int i_bits )
229 {
230     u32         i_result;
231
232     i_result = p_bit_stream->fifo.buffer
233                     >> (8 * sizeof(WORD_TYPE) - i_bits);
234     i_bits = -p_bit_stream->fifo.i_available;
235
236     /* Gather missing bytes. */
237     while( i_bits >= 8 )
238     {
239         if( p_bit_stream->p_byte < p_bit_stream->p_end )
240         {
241             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
242             i_bits -= 8;
243         }
244         else
245         {
246             p_bit_stream->pf_next_data_packet( p_bit_stream );
247             i_result |= *(p_bit_stream->p_byte++) << (i_bits - 8);
248             i_bits -= 8;
249         }
250     }
251
252     /* Gather missing bits. */
253     if( i_bits > 0 )
254     {
255         unsigned int    i_tmp = 8 - i_bits;
256
257         if( p_bit_stream->p_byte < p_bit_stream->p_end )
258         {
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;
263         }
264         else
265         {
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;
271         }
272     }
273     else
274     {
275         p_bit_stream->fifo.i_available = 0;
276         p_bit_stream->fifo.buffer = 0;
277     }
278
279     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
280     {
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 );
287     }
288
289     return( i_result );
290 }
291
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 )
297 {
298     /* First remove all unnecessary bytes. */
299     while( p_bit_stream->fifo.i_available <= -8 )
300     {
301         if( p_bit_stream->p_byte < p_bit_stream->p_end )
302         {
303             p_bit_stream->p_byte++;
304             p_bit_stream->fifo.i_available += 8;
305         }
306         else
307         {
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;
311         }
312     }
313
314     /* Remove unnecessary bits. */
315     if( p_bit_stream->fifo.i_available < 0 )
316     {
317         if( p_bit_stream->p_byte < p_bit_stream->p_end )
318         {
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;
323         }
324         else
325         {
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;
331         }
332     }
333     else
334     {
335         p_bit_stream->fifo.buffer = 0;
336     }
337
338     if( p_bit_stream->p_byte <= p_bit_stream->p_end - sizeof(WORD_TYPE) )
339     {
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 );
346     }
347 }
348