]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
* Fixed a deadlock (the vout unlocked a mutex locked by another thread,
[vlc] / src / audio_decoder / audio_decoder.c
1 /*****************************************************************************
2  * audio_decoder.c: MPEG audio decoder thread
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: audio_decoder.c,v 1.44 2001/01/04 18:47:18 massiot Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Michel Lespinasse <walken@via.ecp.fr>
9  *          Samuel Hocevar <sam@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*
27  * TODO :
28  *
29  * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
30  * - vlc_cond_signal() / vlc_cond_wait() ;
31  *
32  */
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37 #include "defs.h"
38
39 #include <unistd.h>                                              /* getpid() */
40
41 #include <stdio.h>                                           /* "intf_msg.h" */
42 #include <stdlib.h>                                      /* malloc(), free() */
43 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
44 #include <sys/uio.h>                                            /* "input.h" */
45 #include <netinet/in.h>                                             /* ntohl */
46
47 #include "config.h"
48 #include "common.h"
49 #include "threads.h"
50 #include "mtime.h"
51 #include "plugins.h"
52 #include "debug.h"                                      /* "input_netlist.h" */
53
54 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
55  
56 #include "stream_control.h"
57 #include "input_ext-dec.h"
58
59 #include "audio_output.h"               /* aout_fifo_t (for audio_decoder.h) */
60
61 #include "adec_generic.h"
62 #include "audio_decoder.h"
63 #include "adec_math.h"                                     /* DCT32(), PCM() */
64
65 #define ADEC_FRAME_SIZE (2*1152)
66
67 /*****************************************************************************
68  * Local prototypes
69  *****************************************************************************/
70 static int      InitThread             (adec_thread_t * p_adec);
71 static void     RunThread              (adec_thread_t * p_adec);
72 static void     ErrorThread            (adec_thread_t * p_adec);
73 static void     EndThread              (adec_thread_t * p_adec);
74
75 /*****************************************************************************
76  * adec_CreateThread: creates an audio decoder thread
77  *****************************************************************************
78  * This function creates a new audio decoder thread, and returns a pointer to
79  * its description. On error, it returns NULL.
80  *****************************************************************************/
81 vlc_thread_t adec_CreateThread ( adec_config_t * p_config )
82 {
83     adec_thread_t *     p_adec;
84
85     intf_DbgMsg ( "adec debug: creating audio decoder thread" );
86
87     /* Allocate the memory needed to store the thread's structure */
88     if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL ) 
89     {
90         intf_ErrMsg ( "adec error: not enough memory for adec_CreateThread() to create the new thread" );
91         return 0;
92     }
93
94     /*
95      * Initialize the thread properties
96      */
97     p_adec->p_config = p_config;
98     p_adec->p_fifo = p_config->decoder_config.p_decoder_fifo;
99
100
101     /*
102      * Initialize the decoder properties
103      */
104     adec_init ( &p_adec->audio_decoder );
105
106     /*
107      * Initialize the output properties
108      */
109     p_adec->p_aout = p_config->p_aout;
110     p_adec->p_aout_fifo = NULL;
111
112     /* Spawn the audio decoder thread */
113     if ( vlc_thread_create(&p_adec->thread_id, "audio decoder", (vlc_thread_func_t)RunThread, (void *)p_adec) ) 
114     {
115         intf_ErrMsg ("adec error: can't spawn audio decoder thread");
116         free (p_adec);
117         return 0;
118     }
119
120     intf_DbgMsg ("adec debug: audio decoder thread (%p) created", p_adec);
121     return p_adec->thread_id;
122 }
123
124 /*****************************************************************************
125  * InitThread : initialize an audio decoder thread
126  *****************************************************************************
127  * This function is called from RunThread and performs the second step of the
128  * initialization. It returns 0 on success.
129  *****************************************************************************/
130 static int InitThread (adec_thread_t * p_adec)
131 {
132     aout_fifo_t          aout_fifo;
133     adec_byte_stream_t * byte_stream;
134
135     intf_DbgMsg ("adec debug: initializing audio decoder thread %p", p_adec);
136
137     /* Our first job is to initialize the bit stream structure with the
138      * beginning of the input stream */
139     vlc_mutex_lock ( &p_adec->p_fifo->data_lock );
140     while ( DECODER_FIFO_ISEMPTY(*p_adec->p_fifo) ) 
141     {
142         if (p_adec->p_fifo->b_die) 
143         {
144             vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
145             return -1;
146         }
147         vlc_cond_wait ( &p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock );
148     }
149     p_adec->p_data = DECODER_FIFO_START ( *p_adec->p_fifo )->p_first;
150     byte_stream = adec_byte_stream ( &p_adec->audio_decoder );
151     byte_stream->p_byte = p_adec->p_data->p_payload_start;
152     byte_stream->p_end = p_adec->p_data->p_payload_end;
153     byte_stream->info = p_adec;
154     vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
155
156     aout_fifo.i_type = AOUT_ADEC_STEREO_FIFO;
157     aout_fifo.i_channels = 2;
158     aout_fifo.b_stereo = 1;
159     aout_fifo.l_frame_size = ADEC_FRAME_SIZE;
160
161     /* Creating the audio output fifo */
162     if ( (p_adec->p_aout_fifo = aout_CreateFifo(p_adec->p_aout, &aout_fifo)) == NULL ) 
163     {
164         return -1;
165     }
166
167     intf_DbgMsg ( "adec debug: audio decoder thread %p initialized", p_adec );
168     return 0;
169 }
170
171 /*****************************************************************************
172  * RunThread : audio decoder thread
173  *****************************************************************************
174  * Audio decoder thread. This function does only returns when the thread is
175  * terminated.
176  *****************************************************************************/
177 static void RunThread (adec_thread_t * p_adec)
178 {
179     int sync;
180
181     intf_DbgMsg ( "adec debug: running audio decoder thread (%p) (pid == %i)", p_adec, getpid() );
182
183     /* You really suck */
184     //msleep ( INPUT_PTS_DELAY );
185
186     /* Initializing the audio decoder thread */
187     if( InitThread (p_adec) )
188     {
189         p_adec->p_fifo->b_error = 1;
190     }
191
192     sync = 0;
193
194     /* Audio decoder thread's main loop */
195     while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
196     {
197         s16 * buffer;
198         adec_sync_info_t sync_info;
199
200         if ( !sync )
201         {
202             /* have to find a synchro point */
203             adec_byte_stream_t * p_byte_stream;
204             
205             intf_DbgMsg ( "adec: sync" );
206             
207             p_byte_stream = adec_byte_stream ( &p_adec->audio_decoder );
208             /* FIXME: the check will be done later, am I right ? */
209
210             /* FIXME: is this really needed ?
211             adec_byte_stream_next ( p_byte_stream ); */
212
213             if( p_adec->p_fifo->b_die || p_adec->p_fifo->b_error )
214             {
215                 goto bad_frame;
216             }
217
218         }
219
220         if( DECODER_FIFO_START( *p_adec->p_fifo)->i_pts )
221         {
222             p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
223                 DECODER_FIFO_START( *p_adec->p_fifo )->i_pts;
224             DECODER_FIFO_START(*p_adec->p_fifo)->i_pts = 0;
225         }
226         else
227         {
228             p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
229                 LAST_MDATE;
230         }
231
232         if( adec_sync_frame (&p_adec->audio_decoder, &sync_info) )
233         {
234             goto bad_frame;
235         }
236         
237         sync = 1;
238
239         p_adec->p_aout_fifo->l_rate = sync_info.sample_rate;
240
241         buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
242                     + (p_adec->p_aout_fifo->l_end_frame * ADEC_FRAME_SIZE);
243
244         if( adec_decode_frame (&p_adec->audio_decoder, buffer) )
245         {
246             sync = 0;
247             goto bad_frame;
248         }
249
250         vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
251
252         p_adec->p_aout_fifo->l_end_frame =
253             (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
254         vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
255         vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
256
257         bad_frame:
258     }
259
260     /* If b_error is set, the audio decoder thread enters the error loop */
261     if( p_adec->p_fifo->b_error ) 
262     {
263         ErrorThread( p_adec );
264     }
265
266     /* End of the audio decoder thread */
267     EndThread( p_adec );
268 }
269
270 /*****************************************************************************
271  * ErrorThread : audio decoder's RunThread() error loop
272  *****************************************************************************
273  * This function is called when an error occured during thread main's loop. The
274  * thread can still receive feed, but must be ready to terminate as soon as
275  * possible.
276  *****************************************************************************/
277 static void ErrorThread ( adec_thread_t *p_adec )
278 {
279     /* We take the lock, because we are going to read/write the start/end
280      * indexes of the decoder fifo */
281     vlc_mutex_lock ( &p_adec->p_fifo->data_lock );
282
283     /* Wait until a `die' order is sent */
284     while ( !p_adec->p_fifo->b_die ) 
285     {
286         /* Trash all received PES packets */
287         while ( !DECODER_FIFO_ISEMPTY(*p_adec->p_fifo) ) 
288         {
289             p_adec->p_fifo->pf_delete_pes ( p_adec->p_fifo->p_packets_mgt,
290                                    DECODER_FIFO_START(*p_adec->p_fifo) );
291             DECODER_FIFO_INCSTART ( *p_adec->p_fifo );
292         }
293
294         /* Waiting for the input thread to put new PES packets in the fifo */
295         vlc_cond_wait ( &p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock );
296     }
297
298     /* We can release the lock before leaving */
299     vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
300 }
301
302 /*****************************************************************************
303  * EndThread : audio decoder thread destruction
304  *****************************************************************************
305  * This function is called when the thread ends after a sucessful
306  * initialization.
307  *****************************************************************************/
308 static void EndThread ( adec_thread_t *p_adec )
309 {
310     intf_DbgMsg ( "adec debug: destroying audio decoder thread %p", p_adec );
311
312     /* If the audio output fifo was created, we destroy it */
313     if ( p_adec->p_aout_fifo != NULL ) 
314     {
315         aout_DestroyFifo ( p_adec->p_aout_fifo );
316
317         /* Make sure the output thread leaves the NextFrame() function */
318         vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
319         vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
320         vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
321     }
322     /* Destroy descriptor */
323     free( p_adec->p_config );
324     free( p_adec );
325
326     intf_DbgMsg ("adec debug: audio decoder thread %p destroyed", p_adec);
327 }
328
329 void adec_byte_stream_next ( adec_byte_stream_t * p_byte_stream )
330 {
331     adec_thread_t * p_adec = p_byte_stream->info;
332
333     /* We are looking for the next TS packet that contains real data,
334      * and not just a PES header */
335     do 
336     {
337         /* We were reading the last TS packet of this PES packet... It's
338          * time to jump to the next PES packet */
339         if (p_adec->p_data->p_next == NULL) 
340         {
341             /* We are going to read/write the start and end indexes of the
342              * decoder fifo and to use the fifo's conditional variable,
343              * that's why we need to take the lock before */
344             vlc_mutex_lock (&p_adec->p_fifo->data_lock);
345
346             /* Is the input thread dying ? */
347             if (p_adec->p_fifo->b_die) 
348             {
349                 vlc_mutex_unlock (&(p_adec->p_fifo->data_lock));
350                 return;
351             }
352
353             /* We should increase the start index of the decoder fifo, but
354              * if we do this now, the input thread could overwrite the
355              * pointer to the current PES packet, and we weren't able to
356              * give it back to the netlist. That's why we free the PES
357              * packet first. */
358             p_adec->p_fifo->pf_delete_pes (p_adec->p_fifo->p_packets_mgt,
359                     DECODER_FIFO_START(*p_adec->p_fifo));
360             DECODER_FIFO_INCSTART (*p_adec->p_fifo);
361
362             while (DECODER_FIFO_ISEMPTY(*p_adec->p_fifo)) 
363             {
364                 vlc_cond_wait (&p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock);
365                 if (p_adec->p_fifo->b_die) 
366                 {
367                     vlc_mutex_unlock (&(p_adec->p_fifo->data_lock));
368                     return;
369                 }
370             }
371
372             /* The next byte could be found in the next PES packet */
373             p_adec->p_data = DECODER_FIFO_START (*p_adec->p_fifo)->p_first;
374
375             /* We can release the fifo's data lock */
376             vlc_mutex_unlock (&p_adec->p_fifo->data_lock);
377         }
378         /* Perhaps the next TS packet of the current PES packet contains
379          * real data (ie its payload's size is greater than 0) */
380         else 
381         {
382             p_adec->p_data = p_adec->p_data->p_next;
383         }
384     } while (p_adec->p_data->p_payload_start == p_adec->p_data->p_payload_end);
385
386     /* We've found a TS packet which contains interesting data... */
387     p_byte_stream->p_byte = p_adec->p_data->p_payload_start;
388     p_byte_stream->p_end = p_adec->p_data->p_payload_end;
389 }