1 /*****************************************************************************
2 * mpeg_adec.c: MPEG audio decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: mpeg_adec.c,v 1.1 2001/11/13 12:09:18 henri 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 *****************************************************************************/
26 #define MODULE_NAME mpeg_adec
27 #include "modules_inner.h"
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <stdlib.h> /* malloc(), free() */
37 #include "common.h" /* boolean_t, byte_t */
42 #include "audio_output.h" /* aout_fifo_t (for audio_decoder.h) */
45 #include "modules_export.h"
47 #include "stream_control.h"
48 #include "input_ext-dec.h"
50 #include "mpeg_adec_generic.h"
51 #include "mpeg_adec.h"
53 #define ADEC_FRAME_SIZE (2*1152)
55 /*****************************************************************************
57 *****************************************************************************/
58 static int adec_Probe( probedata_t * );
59 static int adec_RunThread ( decoder_config_t * );
60 static void adec_EndThread ( adec_thread_t * );
61 static void adec_ErrorThread ( adec_thread_t * );
62 static void adec_Decode( adec_thread_t * );
65 /*****************************************************************************
67 *****************************************************************************/
68 void _M( adec_getfunctions )( function_list_t * p_function_list )
70 p_function_list->pf_probe = adec_Probe;
71 p_function_list->functions.dec.pf_RunThread = adec_RunThread;
74 /*****************************************************************************
75 * Build configuration tree.
76 *****************************************************************************/
78 ADD_WINDOW( "Configuration for mpeg audio decoder module" )
79 ADD_COMMENT( "Nothing to configure" )
83 p_module->i_capabilities = MODULE_CAPABILITY_DEC;
84 p_module->psz_longname = "Mpeg I layer 1/2 audio decoder";
88 _M( adec_getfunctions )( &p_module->p_functions->dec );
91 MODULE_DEACTIVATE_START
92 MODULE_DEACTIVATE_STOP
94 /*****************************************************************************
95 * adec_Probe: probe the decoder and return score
96 *****************************************************************************/
97 static int adec_Probe( probedata_t *p_data )
99 if( p_data->i_type == MPEG1_AUDIO_ES || p_data->i_type == MPEG2_AUDIO_ES )
105 /*****************************************************************************
106 * adec_RunThread: initialize, go inside main loop, detroy
107 *****************************************************************************/
108 static int adec_RunThread ( decoder_config_t * p_config )
110 adec_thread_t * p_adec;
112 intf_DbgMsg("mpeg_adec debug: thread launched, initializing.");
114 /* Allocate the memory needed to store the thread's structure */
115 if ( (p_adec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL )
117 intf_ErrMsg ( "adec error: not enough memory for"
118 " adec_CreateThread() to create the new thread" );
123 * Initialize the thread properties
125 p_adec->p_config = p_config;
126 p_adec->p_fifo = p_config->p_decoder_fifo;
129 * Initilize the banks
131 p_adec->bank_0.actual = p_adec->bank_0.v1;
132 p_adec->bank_0.pos = 0;
133 p_adec->bank_1.actual = p_adec->bank_1.v1;
134 p_adec->bank_1.pos = 0;
137 * Initialize bit stream
139 p_adec->p_config->pf_init_bit_stream( &p_adec->bit_stream,
140 p_adec->p_config->p_decoder_fifo, NULL, NULL );
142 /* Create the audio output fifo */
143 p_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
144 ADEC_FRAME_SIZE, NULL );
145 if ( p_adec->p_aout_fifo == NULL )
147 intf_ErrMsg("mpeg_adec error: cannot create audio output fifo");
151 intf_DbgMsg("mpeg_adec debug: thread initialized, decoding begins.");
155 /* Audio decoder thread's main loop */
156 while( (!p_adec->p_fifo->b_die) && (!p_adec->p_fifo->b_error) )
158 adec_Decode( p_adec );
161 /* If b_error is set, the audio decoder thread enters the error loop */
162 if( p_adec->p_fifo->b_error )
164 adec_ErrorThread( p_adec );
167 /* End of the audio decoder thread */
168 adec_EndThread( p_adec );
174 * Following finctions are local to this module
177 /*****************************************************************************
178 * adec_Decode: decodes a mpeg frame
179 *****************************************************************************/
180 static void adec_Decode( adec_thread_t * p_adec )
183 adec_sync_info_t sync_info;
185 if( ! adec_SyncFrame (p_adec, &sync_info) )
189 p_adec->p_aout_fifo->l_rate = sync_info.sample_rate;
191 buffer = ((s16 *)p_adec->p_aout_fifo->buffer)
192 + (p_adec->p_aout_fifo->l_end_frame * ADEC_FRAME_SIZE);
194 if( DECODER_FIFO_START( *p_adec->p_fifo)->i_pts )
196 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
197 DECODER_FIFO_START( *p_adec->p_fifo )->i_pts;
198 DECODER_FIFO_START(*p_adec->p_fifo)->i_pts = 0;
202 p_adec->p_aout_fifo->date[p_adec->p_aout_fifo->l_end_frame] =
206 if( adec_DecodeFrame (p_adec, buffer) )
208 /* Ouch, failed decoding... We'll have to resync */
213 vlc_mutex_lock (&p_adec->p_aout_fifo->data_lock);
215 p_adec->p_aout_fifo->l_end_frame =
216 (p_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
217 vlc_cond_signal (&p_adec->p_aout_fifo->data_wait);
218 vlc_mutex_unlock (&p_adec->p_aout_fifo->data_lock);
223 /*****************************************************************************
224 * adec_ErrorThread : audio decoder's RunThread() error loop
225 *****************************************************************************
226 * This function is called when an error occured during thread main's loop. The
227 * thread can still receive feed, but must be ready to terminate as soon as
229 *****************************************************************************/
230 static void adec_ErrorThread ( adec_thread_t *p_adec )
232 /* We take the lock, because we are going to read/write the start/end
233 * indexes of the decoder fifo */
234 vlc_mutex_lock ( &p_adec->p_fifo->data_lock );
236 /* Wait until a `die' order is sent */
237 while ( !p_adec->p_fifo->b_die )
239 /* Trash all received PES packets */
240 while ( !DECODER_FIFO_ISEMPTY(*p_adec->p_fifo) )
242 p_adec->p_fifo->pf_delete_pes ( p_adec->p_fifo->p_packets_mgt,
243 DECODER_FIFO_START(*p_adec->p_fifo) );
244 DECODER_FIFO_INCSTART ( *p_adec->p_fifo );
247 /* Waiting for the input thread to put new PES packets in the fifo */
248 vlc_cond_wait ( &p_adec->p_fifo->data_wait, &p_adec->p_fifo->data_lock );
251 /* We can release the lock before leaving */
252 vlc_mutex_unlock ( &p_adec->p_fifo->data_lock );
256 /*****************************************************************************
257 * adec_EndThread : audio decoder thread destruction
258 *****************************************************************************
259 * This function is called when the thread ends after a sucessful
261 *****************************************************************************/
262 static void adec_EndThread ( adec_thread_t *p_adec )
264 intf_DbgMsg ( "adec debug: destroying audio decoder thread %p", p_adec );
266 /* If the audio output fifo was created, we destroy it */
267 if ( p_adec->p_aout_fifo != NULL )
269 aout_DestroyFifo ( p_adec->p_aout_fifo );
271 /* Make sure the output thread leaves the NextFrame() function */
272 vlc_mutex_lock (&(p_adec->p_aout_fifo->data_lock));
273 vlc_cond_signal (&(p_adec->p_aout_fifo->data_wait));
274 vlc_mutex_unlock (&(p_adec->p_aout_fifo->data_lock));
276 /* Destroy descriptor */
277 free( p_adec->p_config );
280 intf_DbgMsg ("adec debug: audio decoder thread %p destroyed", p_adec);