1 /***************************************************************************
2 mad_adec.c - description
4 Plugin Module definition for using libmad audio decoder in vlc. The
5 libmad codec uses integer arithmic only. This makes it suitable for using
6 it on architectures without a hardware FPU unit, such as the StrongArm
10 copyright : (C) 2001 by Jean-Paul Saman
11 email : jpsaman@wxs.nl
12 ***************************************************************************/
14 /***************************************************************************
16 * This program is free software; you can redistribute it and/or modify *
17 * it under the terms of the GNU General Public License as published by *
18 * the Free Software Foundation; either version 2 of the License, or *
19 * (at your option) any later version. *
21 ***************************************************************************/
23 #define MODULE_NAME mad
24 #include "modules_inner.h"
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
32 #include <string.h> /* strdup() */
35 #include "common.h" /* boolean_t, byte_t */
41 #include "audio_output.h"
44 #include "modules_export.h"
46 #include "stream_control.h"
47 #include "input_ext-dec.h"
51 /*****************************************************************************
52 * Libmad include files *
53 *****************************************************************************/
56 #include "mad_libmad.h"
58 /*****************************************************************************
60 *****************************************************************************/
61 static int decoder_Probe ( probedata_t * );
62 static int decoder_Run ( decoder_config_t * );
63 static int InitThread ( mad_adec_thread_t * p_mad_adec );
64 static void mad_adec_EndThread ( mad_adec_thread_t * p_mad_adec );
66 /*****************************************************************************
68 *****************************************************************************/
69 void _M( adec_getfunctions )( function_list_t * p_function_list )
71 p_function_list->pf_probe = decoder_Probe;
72 p_function_list->functions.dec.pf_run = decoder_Run;
75 /*****************************************************************************
76 * Build configuration tree.
77 *****************************************************************************/
79 ADD_WINDOW( "Configuration for mad_adec module" )
80 ADD_COMMENT( "No device to configure." )
84 p_module->i_capabilities = MODULE_CAPABILITY_DEC;
85 p_module->psz_longname = "Libmad MPEG 1/2/3 audio decoder library";
89 _M( adec_getfunctions )( &p_module->p_functions->dec );
92 MODULE_DEACTIVATE_START
93 MODULE_DEACTIVATE_STOP
95 /*****************************************************************************
96 * decoder_Probe: probe the decoder and return score
97 *****************************************************************************
98 * Tries to launch a decoder and return score so that the interface is able
100 *****************************************************************************/
101 static int decoder_Probe( probedata_t *p_data )
103 if( p_data->i_type == MPEG1_AUDIO_ES || p_data->i_type == MPEG2_AUDIO_ES )
105 if( TestMethod( ADEC_MPEG_VAR, "mad" ) )
117 /*****************************************************************************
118 * decoder_Run: this function is called just after the thread is created
119 *****************************************************************************/
120 static int decoder_Run ( decoder_config_t * p_config )
122 mad_adec_thread_t * p_mad_adec;
124 intf_ErrMsg( "mad_adec debug: mad_adec thread launched, initializing" );
126 /* Allocate the memory needed to store the thread's structure */
127 p_mad_adec = (mad_adec_thread_t *) malloc(sizeof(mad_adec_thread_t));
129 if (p_mad_adec == NULL)
131 intf_ErrMsg ( "mad_adec error: not enough memory "
132 "for decoder_Run() to allocate p_mad_adec" );
133 DecoderError( p_config->p_decoder_fifo );
138 * Initialize the thread properties
140 p_mad_adec->p_config = p_config;
141 p_mad_adec->p_fifo = p_mad_adec->p_config->p_decoder_fifo;
142 if( InitThread( p_mad_adec ) )
144 intf_ErrMsg( "mad_adec error: could not initialize thread" );
145 DecoderError( p_config->p_decoder_fifo );
150 /* mad decoder thread's main loop */
151 while ((!p_mad_adec->p_fifo->b_die) && (!p_mad_adec->p_fifo->b_error))
153 intf_ErrMsg( "mad_adec: starting libmad decoder" );
154 if (mad_decoder_run(p_mad_adec->libmad_decoder, MAD_DECODER_MODE_SYNC)==-1)
156 intf_ErrMsg( "mad_adec error: libmad decoder returns abnormally");
157 DecoderError( p_mad_adec->p_fifo );
158 mad_adec_EndThread(p_mad_adec);
163 /* If b_error is set, the mad decoder thread enters the error loop */
164 if (p_mad_adec->p_fifo->b_error)
166 DecoderError( p_mad_adec->p_fifo );
169 /* End of the ac3 decoder thread */
170 mad_adec_EndThread (p_mad_adec);
175 /*****************************************************************************
176 * InitThread: initialize data before entering main loop
177 *****************************************************************************/
178 static int InitThread( mad_adec_thread_t * p_mad_adec )
181 * Properties of audio for libmad
184 /* Initialize the libmad decoder structures */
185 p_mad_adec->libmad_decoder = (struct mad_decoder*) malloc(sizeof(struct mad_decoder));
188 * Initialize bit stream
190 p_mad_adec->p_config->pf_init_bit_stream( &p_mad_adec->bit_stream,
191 p_mad_adec->p_config->p_decoder_fifo,
192 NULL, /* pf_bitstream_callback */
195 RealignBits( &p_mad_adec->bit_stream );
197 mad_decoder_init( p_mad_adec->libmad_decoder,
198 p_mad_adec, /* vlc's thread structure and p_fifo playbuffer */
199 libmad_input, /* input_func */
200 libmad_header, /* header_func */
202 libmad_output, /* output_func */
206 mad_decoder_options(p_mad_adec->libmad_decoder, MAD_OPTION_IGNORECRC);
209 * Initialize the output properties
212 /* Creating the audio output fifo */
213 p_mad_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, /* fifo type */
214 2, /* nr. of channels */
215 48000, /* frame rate in Hz ?*/
217 ADEC_FRAME_SIZE/2, /* frame size */
220 if ( p_mad_adec->p_aout_fifo == NULL )
225 intf_ErrMsg("mad_adec debug: mad decoder thread %p initialized", p_mad_adec);
231 /*****************************************************************************
232 * mad_adec_EndThread : libmad decoder thread destruction
233 *****************************************************************************/
234 static void mad_adec_EndThread (mad_adec_thread_t * p_mad_adec)
236 intf_ErrMsg ("mad_adec debug: destroying mad decoder thread %p", p_mad_adec);
238 /* If the audio output fifo was created, we destroy it */
239 if (p_mad_adec->p_aout_fifo != NULL)
241 aout_DestroyFifo (p_mad_adec->p_aout_fifo);
243 /* Make sure the output thread leaves the NextFrame() function */
244 vlc_mutex_lock (&(p_mad_adec->p_aout_fifo->data_lock));
245 vlc_cond_signal (&(p_mad_adec->p_aout_fifo->data_wait));
246 vlc_mutex_unlock (&(p_mad_adec->p_aout_fifo->data_lock));
249 /* mad_decoder_finish releases the memory allocated inside the struct */
250 mad_decoder_finish( p_mad_adec->libmad_decoder );
252 /* Unlock the modules */
253 free( p_mad_adec->libmad_decoder );
254 // free( p_mad_adec->p_config ); /* for now a reminder until integration with cvs */
257 intf_ErrMsg ("mad_adec debug: mad decoder thread %p destroyed", p_mad_adec);