* Mandatory step for video output IV and the audio output quality
[vlc.git] / src / audio_decoder / audio_decoder.c
blob763c3aac829147ed700dd8a3697dec787ee7a27b
1 /*****************************************************************************
2 * audio_decoder.c: MPEG audio decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: audio_decoder.c,v 1.50 2001/05/01 04:18:18 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Michel Lespinasse <walken@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
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.
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.
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 *****************************************************************************/
27 * TODO :
29 * - optimiser les NeedBits() et les GetBits() du code là où c'est possible ;
30 * - vlc_cond_signal() / vlc_cond_wait() ;
34 /*****************************************************************************
35 * Preamble
36 *****************************************************************************/
37 #include "defs.h"
39 #include <unistd.h> /* getpid() */
41 #include <stdio.h> /* "intf_msg.h" */
42 #include <string.h> /* memcpy(), memset() */
43 #include <stdlib.h> /* malloc(), free() */
45 #include "config.h"
46 #include "common.h"
47 #include "threads.h"
48 #include "mtime.h"
50 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
52 #include "stream_control.h"
53 #include "input_ext-dec.h"
55 #include "audio_output.h" /* aout_fifo_t (for audio_decoder.h) */
57 #include "adec_generic.h"
58 #include "audio_decoder.h"
59 #include "adec_math.h" /* DCT32(), PCM() */
61 #define ADEC_FRAME_SIZE (2*1152)
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);
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 )
79 adec_thread_t * p_adec;
81 intf_DbgMsg ( "adec debug: creating audio decoder thread" );
83 /* Allocate the memory needed to store the thread's structure */
84 if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL )
86 intf_ErrMsg ( "adec error: not enough memory for"
87 " adec_CreateThread() to create the new thread" );
88 return 0;
92 * Initialize the thread properties
94 p_adec->p_config = p_config;
95 p_adec->p_fifo = p_config->decoder_config.p_decoder_fifo;
98 * Initialize the decoder properties
100 adec_Init ( p_adec );
103 * Initialize the output properties
105 p_adec->p_aout_fifo = NULL;
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) )
111 intf_ErrMsg ("adec error: can't spawn audio decoder thread");
112 free (p_adec);
113 return 0;
116 intf_DbgMsg ("adec debug: audio decoder thread (%p) created", p_adec);
117 return p_adec->thread_id;
120 /* following functions are local */
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)
130 intf_DbgMsg ("adec debug: initializing audio decoder thread %p", p_adec);
132 p_adec->p_config->decoder_config.pf_init_bit_stream( &p_adec->bit_stream,
133 p_adec->p_config->decoder_config.p_decoder_fifo, NULL, NULL );
135 /* Creating the audio output fifo */
136 p_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
137 ADEC_FRAME_SIZE, NULL );
138 if ( p_adec->p_aout_fifo == NULL )
140 return -1;
143 intf_DbgMsg ( "adec debug: audio decoder thread %p initialized", p_adec );
144 return 0;
147 /*****************************************************************************
148 * RunThread : audio decoder thread
149 *****************************************************************************
150 * Audio decoder thread. This function does only returns when the thread is
151 * terminated.
152 *****************************************************************************/
153 static void RunThread (adec_thread_t * p_adec)
155 int sync;
157 intf_DbgMsg ( "adec debug: running audio decoder thread (%p) (pid == %i)",
158 p_adec, getpid() );
160 /* Initializing the audio decoder thread */
161 p_adec->p_fifo->b_error = InitThread (p_adec);
163 sync = 0;
165 /* Audio decoder thread's main loop */
166 while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
168 s16 * buffer;
169 adec_sync_info_t sync_info;
171 if( DECODER_FIFO_START( *p_adec->p_fifo)->i_pts )
173 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
174 DECODER_FIFO_START( *p_adec->p_fifo )->i_pts;
175 DECODER_FIFO_START(*p_adec->p_fifo)->i_pts = 0;
177 else
179 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
180 LAST_MDATE;
183 if( ! adec_SyncFrame (p_adec, &sync_info) )
185 sync = 1;
187 p_adec->p_aout_fifo->l_rate = sync_info.sample_rate;
189 buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
190 + (p_adec->p_aout_fifo->l_end_frame * ADEC_FRAME_SIZE);
192 if( adec_DecodeFrame (p_adec, buffer) )
194 /* Ouch, failed decoding... We'll have to resync */
195 sync = 0;
197 else
199 vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
201 p_adec->p_aout_fifo->l_end_frame =
202 (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
203 vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
204 vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
209 /* If b_error is set, the audio decoder thread enters the error loop */
210 if( p_adec->p_fifo->b_error )
212 ErrorThread( p_adec );
215 /* End of the audio decoder thread */
216 EndThread( p_adec );
219 /*****************************************************************************
220 * ErrorThread : audio decoder's RunThread() error loop
221 *****************************************************************************
222 * This function is called when an error occured during thread main's loop. The
223 * thread can still receive feed, but must be ready to terminate as soon as
224 * possible.
225 *****************************************************************************/
226 static void ErrorThread ( adec_thread_t *p_adec )
228 /* We take the lock, because we are going to read/write the start/end
229 * indexes of the decoder fifo */
230 vlc_mutex_lock ( &p_adec->p_fifo->data_lock );
232 /* Wait until a `die' order is sent */
233 while ( !p_adec->p_fifo->b_die )
235 /* Trash all received PES packets */
236 while ( !DECODER_FIFO_ISEMPTY(*p_adec->p_fifo) )
238 p_adec->p_fifo->pf_delete_pes ( p_adec->p_fifo->p_packets_mgt,
239 DECODER_FIFO_START(*p_adec->p_fifo) );
240 DECODER_FIFO_INCSTART ( *p_adec->p_fifo );
243 /* Waiting for the input thread to put new PES packets in the fifo */
244 vlc_cond_wait ( &p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock );
247 /* We can release the lock before leaving */
248 vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
251 /*****************************************************************************
252 * EndThread : audio decoder thread destruction
253 *****************************************************************************
254 * This function is called when the thread ends after a sucessful
255 * initialization.
256 *****************************************************************************/
257 static void EndThread ( adec_thread_t *p_adec )
259 intf_DbgMsg ( "adec debug: destroying audio decoder thread %p", p_adec );
261 /* If the audio output fifo was created, we destroy it */
262 if ( p_adec->p_aout_fifo != NULL )
264 aout_DestroyFifo ( p_adec->p_aout_fifo );
266 /* Make sure the output thread leaves the NextFrame() function */
267 vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
268 vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
269 vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
271 /* Destroy descriptor */
272 free( p_adec->p_config );
273 free( p_adec );
275 intf_DbgMsg ("adec debug: audio decoder thread %p destroyed", p_adec);