]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_decoder_thread.c
oops, j'ai oublie un fichier
[vlc] / src / ac3_decoder / ac3_decoder_thread.c
1 /*****************************************************************************
2  * ac3_decoder.c: ac3 decoder thread
3  * (c)1999 VideoLAN
4  *****************************************************************************/
5
6 /*
7  * TODO :
8  *
9  * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
10  *   décodés ;
11  * - vlc_cond_signal() / vlc_cond_wait()
12  *
13  */
14
15 /*****************************************************************************
16  * Preamble
17  *****************************************************************************/
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21
22 #include <unistd.h>                                              /* getpid() */
23
24 #include <stdio.h>                                           /* "intf_msg.h" */
25 #include <stdlib.h>                                      /* malloc(), free() */
26 #include <sys/soundcard.h>                               /* "audio_output.h" */
27 #include <sys/uio.h>                                            /* "input.h" */
28
29 #include "common.h"
30 #include "config.h"
31 #include "mtime.h"
32 #include "vlc_thread.h"
33 #include "debug.h"                                      /* "input_netlist.h" */
34
35 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
36
37 #include "input.h"                                           /* pes_packet_t */
38 #include "input_netlist.h"                         /* input_NetlistFreePES() */
39 #include "decoder_fifo.h"         /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
40
41 #include "audio_output.h"
42
43 #include "ac3_decoder.h"
44 #include "ac3_decoder_thread.h"
45 #include "ac3_parse.h"
46 #include "ac3_imdct.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int      InitThread              ( ac3dec_thread_t * p_adec );
52 static void     RunThread               ( ac3dec_thread_t * p_adec );
53 static void     ErrorThread             ( ac3dec_thread_t * p_adec );
54 static void     EndThread               ( ac3dec_thread_t * p_adec );
55
56 /*****************************************************************************
57  * ac3dec_CreateThread: creates an ac3 decoder thread
58  *****************************************************************************/
59 ac3dec_thread_t * ac3dec_CreateThread( input_thread_t * p_input )
60 {
61     ac3dec_thread_t *   p_ac3dec;
62
63     intf_DbgMsg("ac3dec debug: creating ac3 decoder thread\n");
64
65     /* Allocate the memory needed to store the thread's structure */
66     if ( (p_ac3dec = (ac3dec_thread_t *)malloc( sizeof(ac3dec_thread_t) )) == NULL )
67     {
68         intf_ErrMsg("ac3dec error: not enough memory for ac3dec_CreateThread() to create the new thread\n");
69         return( NULL );
70     }
71
72     /*
73      * Initialize the thread properties
74      */
75     p_ac3dec->b_die = 0;
76     p_ac3dec->b_error = 0;
77
78     /*
79      * Initialize the input properties
80      */
81     /* Initialize the decoder fifo's data lock and conditional variable and set
82      * its buffer as empty */
83     vlc_mutex_init( &p_ac3dec->fifo.data_lock );
84     vlc_cond_init( &p_ac3dec->fifo.data_wait );
85     p_ac3dec->fifo.i_start = 0;
86     p_ac3dec->fifo.i_end = 0;
87     /* Initialize the bit stream structure */
88     p_ac3dec->ac3_decoder.bit_stream.p_input = p_input;
89     p_ac3dec->ac3_decoder.bit_stream.p_decoder_fifo = &p_ac3dec->fifo;
90     p_ac3dec->ac3_decoder.bit_stream.fifo.buffer = 0;
91     p_ac3dec->ac3_decoder.bit_stream.fifo.i_available = 0;
92
93     /*
94      * Initialize the output properties
95      */
96     p_ac3dec->p_aout = p_input->p_aout;
97     p_ac3dec->p_aout_fifo = NULL;
98
99     imdct_init();
100
101     /* Spawn the ac3 decoder thread */
102     if ( vlc_thread_create(&p_ac3dec->thread_id, "ac3 decoder", (vlc_thread_func_t)RunThread, (void *)p_ac3dec) )
103     {
104         intf_ErrMsg( "ac3dec error: can't spawn ac3 decoder thread\n" );
105         free( p_ac3dec );
106         return( NULL );
107     }
108
109     intf_DbgMsg( "ac3dec debug: ac3 decoder thread (%p) created\n", p_ac3dec );
110     return( p_ac3dec );
111 }
112
113 /*****************************************************************************
114  * ac3dec_DestroyThread: destroys an ac3 decoder thread
115  *****************************************************************************/
116 void ac3dec_DestroyThread( ac3dec_thread_t * p_ac3dec )
117 {
118     intf_DbgMsg( "ac3dec debug: requesting termination of ac3 decoder thread %p\n", p_ac3dec );
119
120     /* Ask thread to kill itself */
121     p_ac3dec->b_die = 1;
122
123     /* Make sure the decoder thread leaves the GetByte() function */
124     vlc_mutex_lock( &(p_ac3dec->fifo.data_lock) );
125     vlc_cond_signal( &(p_ac3dec->fifo.data_wait) );
126     vlc_mutex_unlock( &(p_ac3dec->fifo.data_lock) );
127
128     /* Waiting for the decoder thread to exit */
129     /* Remove this as soon as the "status" flag is implemented */
130     vlc_thread_join( p_ac3dec->thread_id );
131 }
132
133 /* Following functions are local */
134
135 /*****************************************************************************
136  * decode_find_sync()
137  *****************************************************************************/
138 static __inline__ int decode_find_sync( ac3dec_thread_t * p_ac3dec )
139 {
140     while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
141     {
142         NeedBits( &(p_ac3dec->ac3_decoder.bit_stream), 16 );
143         if ( (p_ac3dec->ac3_decoder.bit_stream.fifo.buffer >> (32 - 16)) == 0x0b77 )
144         {
145             DumpBits( &(p_ac3dec->ac3_decoder.bit_stream), 16 );
146             p_ac3dec->ac3_decoder.total_bits_read = 16;
147             return( 0 );
148         }
149         DumpBits( &(p_ac3dec->ac3_decoder.bit_stream), 1 ); /* XXX */
150     }
151     return( -1 );
152 }
153
154 /*****************************************************************************
155  * InitThread : initialize an ac3 decoder thread
156  *****************************************************************************/
157 static int InitThread( ac3dec_thread_t * p_ac3dec )
158 {
159     aout_fifo_t         aout_fifo;
160
161     intf_DbgMsg( "ac3dec debug: initializing ac3 decoder thread %p\n", p_ac3dec );
162
163     /* Our first job is to initialize the bit stream structure with the
164      * beginning of the input stream */
165     vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
166     while ( DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
167     {
168         if ( p_ac3dec->b_die )
169         {
170             vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
171             return( -1 );
172         }
173         vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
174     }
175     p_ac3dec->ac3_decoder.bit_stream.p_ts = DECODER_FIFO_START( p_ac3dec->fifo )->p_first_ts;
176     p_ac3dec->ac3_decoder.bit_stream.p_byte = p_ac3dec->ac3_decoder.bit_stream.p_ts->buffer + p_ac3dec->ac3_decoder.bit_stream.p_ts->i_payload_start;
177     p_ac3dec->ac3_decoder.bit_stream.p_end = p_ac3dec->ac3_decoder.bit_stream.p_ts->buffer + p_ac3dec->ac3_decoder.bit_stream.p_ts->i_payload_end;
178     vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
179
180     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
181     aout_fifo.i_channels = 2;
182     aout_fifo.b_stereo = 1;
183
184     aout_fifo.l_frame_size = AC3DEC_FRAME_SIZE;
185
186     /* Creating the audio output fifo */
187     if ( (p_ac3dec->p_aout_fifo = aout_CreateFifo(p_ac3dec->p_aout, &aout_fifo)) == NULL )
188     {
189         return( -1 );
190     }
191
192     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p initialized\n", p_ac3dec );
193     return( 0 );
194 }
195
196 /*****************************************************************************
197  * RunThread : ac3 decoder thread
198  *****************************************************************************/
199 static void RunThread( ac3dec_thread_t * p_ac3dec )
200 {
201     intf_DbgMsg( "ac3dec debug: running ac3 decoder thread (%p) (pid == %i)\n", p_ac3dec, getpid() );
202
203     msleep( INPUT_PTS_DELAY );
204
205     /* Initializing the ac3 decoder thread */
206     if ( InitThread(p_ac3dec) ) /* XXX */
207     {
208         p_ac3dec->b_error = 1;
209     }
210
211     /* ac3 decoder thread's main loop */
212     /* FIXME : do we have enough room to store the decoded frames ? */
213     while ( (!p_ac3dec->b_die) && (!p_ac3dec->b_error) )
214     {
215         int i;
216
217         p_ac3dec->ac3_decoder.b_invalid = 0;
218
219         decode_find_sync( p_ac3dec );
220
221         if ( DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts )
222         {
223                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = DECODER_FIFO_START(p_ac3dec->fifo)->i_pts;
224                 DECODER_FIFO_START(p_ac3dec->fifo)->b_has_pts = 0;
225         }
226         else
227         {
228                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
229         }
230
231         parse_syncinfo( &p_ac3dec->ac3_decoder );
232         switch ( p_ac3dec->ac3_decoder.syncinfo.fscod )
233         {
234                 case 0:
235                         p_ac3dec->p_aout_fifo->l_rate = 48000;
236                         break;
237
238                 case 1:
239                         p_ac3dec->p_aout_fifo->l_rate = 44100;
240                         break;
241
242                 case 2:
243                         p_ac3dec->p_aout_fifo->l_rate = 32000;
244                         break;
245
246                 default: /* XXX */
247                         fprintf( stderr, "ac3dec debug: invalid fscod\n" );
248                         p_ac3dec->ac3_decoder.b_invalid = 1;
249                         break;
250         }
251         if ( p_ac3dec->ac3_decoder.b_invalid ) /* XXX */
252         {
253                 continue;
254         }
255
256         parse_bsi( &p_ac3dec->ac3_decoder );
257
258         for (i = 0; i < 6; i++)
259             {
260             s16 * buffer;
261
262             buffer = ((ac3dec_frame_t *)p_ac3dec->p_aout_fifo->buffer)[ p_ac3dec->p_aout_fifo->l_end_frame ];
263
264             if (ac3_audio_block (&p_ac3dec->ac3_decoder, buffer))
265                 goto bad_frame;
266
267             if (i)
268                 p_ac3dec->p_aout_fifo->date[p_ac3dec->p_aout_fifo->l_end_frame] = LAST_MDATE;
269             vlc_mutex_lock( &p_ac3dec->p_aout_fifo->data_lock );
270             p_ac3dec->p_aout_fifo->l_end_frame = (p_ac3dec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
271             vlc_cond_signal( &p_ac3dec->p_aout_fifo->data_wait );
272             vlc_mutex_unlock( &p_ac3dec->p_aout_fifo->data_lock );
273         }
274
275         parse_auxdata( &p_ac3dec->ac3_decoder );
276 bad_frame:
277     }
278
279     /* If b_error is set, the ac3 decoder thread enters the error loop */
280     if ( p_ac3dec->b_error )
281     {
282         ErrorThread( p_ac3dec );
283     }
284
285     /* End of the ac3 decoder thread */
286     EndThread( p_ac3dec );
287 }
288
289 /*****************************************************************************
290  * ErrorThread : ac3 decoder's RunThread() error loop
291  *****************************************************************************/
292 static void ErrorThread( ac3dec_thread_t * p_ac3dec )
293 {
294     /* We take the lock, because we are going to read/write the start/end
295      * indexes of the decoder fifo */
296     vlc_mutex_lock( &p_ac3dec->fifo.data_lock );
297
298     /* Wait until a `die' order is sent */
299     while( !p_ac3dec->b_die )
300     {
301         /* Trash all received PES packets */
302         while( !DECODER_FIFO_ISEMPTY(p_ac3dec->fifo) )
303         {
304             input_NetlistFreePES( p_ac3dec->ac3_decoder.bit_stream.p_input, DECODER_FIFO_START(p_ac3dec->fifo) );
305             DECODER_FIFO_INCSTART( p_ac3dec->fifo );
306         }
307
308         /* Waiting for the input thread to put new PES packets in the fifo */
309         vlc_cond_wait( &p_ac3dec->fifo.data_wait, &p_ac3dec->fifo.data_lock );
310     }
311
312     /* We can release the lock before leaving */
313     vlc_mutex_unlock( &p_ac3dec->fifo.data_lock );
314 }
315
316 /*****************************************************************************
317  * EndThread : ac3 decoder thread destruction
318  *****************************************************************************/
319 static void EndThread( ac3dec_thread_t * p_ac3dec )
320 {
321     intf_DbgMsg( "ac3dec debug: destroying ac3 decoder thread %p\n", p_ac3dec );
322
323     /* If the audio output fifo was created, we destroy it */
324     if ( p_ac3dec->p_aout_fifo != NULL )
325     {
326         aout_DestroyFifo( p_ac3dec->p_aout_fifo );
327
328         /* Make sure the output thread leaves the NextFrame() function */
329         vlc_mutex_lock( &(p_ac3dec->p_aout_fifo->data_lock) );
330         vlc_cond_signal( &(p_ac3dec->p_aout_fifo->data_wait) );
331         vlc_mutex_unlock( &(p_ac3dec->p_aout_fifo->data_lock) );
332     }
333
334     /* Destroy descriptor */
335     free( p_ac3dec );
336
337     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed\n", p_ac3dec );
338 }