]> git.sesse.net Git - vlc/blob - src/audio_decoder/audio_decoder.c
* Totally rewrote the video decoder (inspired by walken's mpeg2dec), implying :
[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.52 2001/08/22 17:21:45 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 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>                                              /* getpid() */
41 #endif
42
43 #include <stdio.h>                                           /* "intf_msg.h" */
44 #include <string.h>                                    /* memcpy(), memset() */
45 #include <stdlib.h>                                      /* malloc(), free() */
46
47 #include "config.h"
48 #include "common.h"
49 #include "threads.h"
50 #include "mtime.h"
51
52 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
53  
54 #include "stream_control.h"
55 #include "input_ext-dec.h"
56
57 #include "audio_output.h"               /* aout_fifo_t (for audio_decoder.h) */
58
59 #include "adec_generic.h"
60 #include "audio_decoder.h"
61 #include "adec_math.h"                                     /* DCT32(), PCM() */
62
63 #define ADEC_FRAME_SIZE (2*1152)
64
65 /*****************************************************************************
66  * Local prototypes
67  *****************************************************************************/
68 static int      InitThread             (adec_thread_t * p_adec);
69 static void     RunThread              (adec_thread_t * p_adec);
70 static void     ErrorThread            (adec_thread_t * p_adec);
71 static void     EndThread              (adec_thread_t * p_adec);
72
73 /*****************************************************************************
74  * adec_CreateThread: creates an audio decoder thread
75  *****************************************************************************
76  * This function creates a new audio decoder thread, and returns a pointer to
77  * its description. On error, it returns NULL.
78  *****************************************************************************/
79 vlc_thread_t adec_CreateThread ( adec_config_t * p_config )
80 {
81     adec_thread_t *     p_adec;
82
83     intf_DbgMsg ( "adec debug: creating audio decoder thread" );
84
85     /* Allocate the memory needed to store the thread's structure */
86     if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL ) 
87     {
88         intf_ErrMsg ( "adec error: not enough memory for"
89                       " adec_CreateThread() to create the new thread" );
90         return 0;
91     }
92
93     /*
94      * Initialize the thread properties
95      */
96     p_adec->p_config = p_config;
97     p_adec->p_fifo = p_config->decoder_config.p_decoder_fifo;
98
99     /*
100      * Initialize the decoder properties
101      */
102     adec_Init ( p_adec );
103
104     /*
105      * Initialize the output properties
106      */
107     p_adec->p_aout_fifo = NULL;
108
109     /* Spawn the audio decoder thread */
110     if ( vlc_thread_create(&p_adec->thread_id, "audio decoder",
111          (vlc_thread_func_t)RunThread, (void *)p_adec) ) 
112     {
113         intf_ErrMsg ("adec error: can't spawn audio decoder thread");
114         free (p_adec);
115         return 0;
116     }
117
118     intf_DbgMsg ("adec debug: audio decoder thread (%p) created", p_adec);
119     return p_adec->thread_id;
120 }
121
122 /* following functions are local */
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     intf_DbgMsg ("adec debug: initializing audio decoder thread %p", p_adec);
133
134     p_adec->p_config->decoder_config.pf_init_bit_stream( &p_adec->bit_stream,
135         p_adec->p_config->decoder_config.p_decoder_fifo, NULL, NULL );
136
137     /* Creating the audio output fifo */
138     p_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
139                                            ADEC_FRAME_SIZE, NULL );
140     if ( p_adec->p_aout_fifo == NULL ) 
141     {
142         return -1;
143     }
144
145     intf_DbgMsg ( "adec debug: audio decoder thread %p initialized", p_adec );
146     return 0;
147 }
148
149 /*****************************************************************************
150  * RunThread : audio decoder thread
151  *****************************************************************************
152  * Audio decoder thread. This function does only returns when the thread is
153  * terminated.
154  *****************************************************************************/
155 static void RunThread (adec_thread_t * p_adec)
156 {
157     int sync;
158
159     intf_DbgMsg ( "adec debug: running audio decoder thread (%p) (pid == %i)",
160                   p_adec, getpid() );
161
162     /* Initializing the audio decoder thread */
163     p_adec->p_fifo->b_error = InitThread (p_adec);
164
165     sync = 0;
166
167     /* Audio decoder thread's main loop */
168     while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
169     {
170         s16 * buffer;
171         adec_sync_info_t sync_info;
172
173         if( ! adec_SyncFrame (p_adec, &sync_info) )
174         {
175             sync = 1;
176
177             p_adec->p_aout_fifo->l_rate = sync_info.sample_rate;
178
179             buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
180                         + (p_adec->p_aout_fifo->l_end_frame * ADEC_FRAME_SIZE);
181
182             if( DECODER_FIFO_START( *p_adec->p_fifo)->i_pts )
183             {
184                 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
185                     DECODER_FIFO_START( *p_adec->p_fifo )->i_pts;
186                 DECODER_FIFO_START(*p_adec->p_fifo)->i_pts = 0;
187             }
188             else
189             {
190                 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
191                     LAST_MDATE;
192             }
193
194             if( adec_DecodeFrame (p_adec, buffer) )
195             {
196                 /* Ouch, failed decoding... We'll have to resync */
197                 sync = 0;
198             }
199             else
200             {
201                 vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
202     
203                 p_adec->p_aout_fifo->l_end_frame =
204                     (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
205                 vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
206                 vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
207             }
208         }
209     }
210
211     /* If b_error is set, the audio decoder thread enters the error loop */
212     if( p_adec->p_fifo->b_error ) 
213     {
214         ErrorThread( p_adec );
215     }
216
217     /* End of the audio decoder thread */
218     EndThread( p_adec );
219 }
220
221 /*****************************************************************************
222  * ErrorThread : audio decoder's RunThread() error loop
223  *****************************************************************************
224  * This function is called when an error occured during thread main's loop. The
225  * thread can still receive feed, but must be ready to terminate as soon as
226  * possible.
227  *****************************************************************************/
228 static void ErrorThread ( adec_thread_t *p_adec )
229 {
230     /* We take the lock, because we are going to read/write the start/end
231      * indexes of the decoder fifo */
232     vlc_mutex_lock ( &p_adec->p_fifo->data_lock );
233
234     /* Wait until a `die' order is sent */
235     while ( !p_adec->p_fifo->b_die ) 
236     {
237         /* Trash all received PES packets */
238         while ( !DECODER_FIFO_ISEMPTY(*p_adec->p_fifo) ) 
239         {
240             p_adec->p_fifo->pf_delete_pes ( p_adec->p_fifo->p_packets_mgt,
241                                    DECODER_FIFO_START(*p_adec->p_fifo) );
242             DECODER_FIFO_INCSTART ( *p_adec->p_fifo );
243         }
244
245         /* Waiting for the input thread to put new PES packets in the fifo */
246         vlc_cond_wait ( &p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock );
247     }
248
249     /* We can release the lock before leaving */
250     vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
251 }
252
253 /*****************************************************************************
254  * EndThread : audio decoder thread destruction
255  *****************************************************************************
256  * This function is called when the thread ends after a sucessful
257  * initialization.
258  *****************************************************************************/
259 static void EndThread ( adec_thread_t *p_adec )
260 {
261     intf_DbgMsg ( "adec debug: destroying audio decoder thread %p", p_adec );
262
263     /* If the audio output fifo was created, we destroy it */
264     if ( p_adec->p_aout_fifo != NULL ) 
265     {
266         aout_DestroyFifo ( p_adec->p_aout_fifo );
267
268         /* Make sure the output thread leaves the NextFrame() function */
269         vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
270         vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
271         vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
272     }
273     /* Destroy descriptor */
274     free( p_adec->p_config );
275     free( p_adec );
276
277     intf_DbgMsg ("adec debug: audio decoder thread %p destroyed", p_adec);
278 }
279