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