1 /*****************************************************************************
2 * mpeg_adec.c: MPEG audio decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: mpeg_adec.c,v 1.26 2002/07/23 00:39:17 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Michel Lespinasse <walken@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Cyril Deguet <asmax@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
34 #include <vlc/decoder.h>
37 #include "mpeg_adec_generic.h"
38 #include "mpeg_adec.h"
40 #define ADEC_FRAME_SIZE (2*1152)
42 /*****************************************************************************
44 *****************************************************************************/
45 static int decoder_Probe ( vlc_fourcc_t * );
46 static int decoder_Run ( decoder_fifo_t * );
47 static void EndThread ( adec_thread_t * );
48 static void DecodeThread ( adec_thread_t * );
50 /*****************************************************************************
52 *****************************************************************************/
53 void _M( adec_getfunctions )( function_list_t * p_function_list )
55 p_function_list->functions.dec.pf_probe = decoder_Probe;
56 p_function_list->functions.dec.pf_run = decoder_Run;
59 /*****************************************************************************
60 * Build configuration tree.
61 *****************************************************************************/
66 SET_DESCRIPTION( _("MPEG I/II layer 1/2 audio decoder") )
67 ADD_CAPABILITY( DECODER, 50 )
68 ADD_REQUIREMENT( FPU )
69 ADD_SHORTCUT( "builtin" )
73 _M( adec_getfunctions )( &p_module->p_functions->dec );
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
79 /*****************************************************************************
80 * decoder_Probe: probe the decoder and return score
81 *****************************************************************************/
82 static int decoder_Probe( vlc_fourcc_t *pi_type )
84 return *pi_type == VLC_FOURCC('m','p','g','a') ? 0 : -1;
87 /*****************************************************************************
88 * decoder_Run: initialize, go inside main loop, detroy
89 *****************************************************************************/
90 static int decoder_Run ( decoder_fifo_t * p_fifo )
92 adec_thread_t * p_adec;
94 /* Allocate the memory needed to store the thread's structure */
95 if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL )
97 msg_Err( p_fifo, "out of memory" );
98 DecoderError( p_fifo );
103 * Initialize the thread properties
105 p_adec->p_fifo = p_fifo;
108 * Initilize the banks
110 p_adec->bank_0.actual = p_adec->bank_0.v1;
111 p_adec->bank_0.pos = 0;
112 p_adec->bank_1.actual = p_adec->bank_1.v1;
113 p_adec->bank_1.pos = 0;
116 * Initialize bit stream
118 InitBitstream( &p_adec->bit_stream, p_adec->p_fifo, NULL, NULL );
120 /* We do not create the audio output fifo now, but
121 it will be created when the first frame is received */
122 p_adec->p_aout_fifo = NULL;
126 /* Audio decoder thread's main loop */
127 while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
129 DecodeThread( p_adec );
132 /* If b_error is set, the audio decoder thread enters the error loop */
133 if( p_adec->p_fifo->b_error )
135 DecoderError( p_adec->p_fifo );
138 /* End of the audio decoder thread */
145 * Following functions are local to this module
148 /*****************************************************************************
149 * DecodeThread: decodes a mpeg frame
150 *****************************************************************************/
151 static void DecodeThread( adec_thread_t * p_adec )
154 adec_sync_info_t sync_info;
156 if( ! adec_SyncFrame (p_adec, &sync_info) )
159 /* TODO: check if audio type has changed */
161 /* Create the output fifo if it doesn't exist yet */
162 if( p_adec->p_aout_fifo == NULL )
166 if( !config_GetInt( p_adec->p_fifo, "mono" ) )
168 msg_Dbg( p_adec->p_fifo, "setting stereo output" );
171 else if( sync_info.b_stereo )
179 p_adec->p_aout_fifo =
180 aout_CreateFifo( p_adec->p_fifo, AOUT_FIFO_PCM, i_channels,
181 sync_info.sample_rate, ADEC_FRAME_SIZE, NULL );
182 if( p_adec->p_aout_fifo == NULL)
184 msg_Err( p_adec->p_fifo, "failed to create aout fifo" );
185 p_adec->p_fifo->b_error = 1;
192 p_buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
193 + (p_adec->p_aout_fifo->i_end_frame * ADEC_FRAME_SIZE);
195 CurrentPTS( &p_adec->bit_stream,
196 &p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame],
198 if( !p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame] )
200 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->i_end_frame] =
204 if( adec_DecodeFrame (p_adec, p_buffer) )
206 /* Ouch, failed decoding... We'll have to resync */
211 vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
212 p_adec->p_aout_fifo->i_end_frame =
213 (p_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
214 vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
215 vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
220 /*****************************************************************************
221 * EndThread : audio decoder thread destruction
222 *****************************************************************************
223 * This function is called when the thread ends after a sucessful
225 *****************************************************************************/
226 static void EndThread ( adec_thread_t *p_adec )
228 /* If the audio output fifo was created, we destroy it */
229 if ( p_adec->p_aout_fifo != NULL )
231 aout_DestroyFifo ( p_adec->p_aout_fifo );
233 /* Make sure the output thread leaves the NextFrame() function */
234 vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
235 vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
236 vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
238 /* Destroy descriptor */