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