1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_adec.c,v 1.14 2002/01/14 23:46:35 massiot Exp $
7 * Authors: Michel Lespinasse <walken@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memset() */
30 #include <videolan/vlc.h>
33 # include <unistd.h> /* getpid() */
36 #include "audio_output.h"
38 #include "stream_control.h"
39 #include "input_ext-dec.h"
40 #include "input_ext-intf.h" /* MPEG?_AUDIO_ES */
42 #include "ac3_imdct.h"
43 #include "ac3_downmix.h"
44 #include "ac3_decoder.h"
47 #define AC3DEC_FRAME_SIZE (2*1536)
49 /*****************************************************************************
51 *****************************************************************************/
52 static int decoder_Probe ( probedata_t * );
53 static int decoder_Run ( decoder_config_t * );
54 static int InitThread ( ac3dec_thread_t * p_adec );
55 static void EndThread ( ac3dec_thread_t * p_adec );
56 static void BitstreamCallback ( bit_stream_t *p_bit_stream,
57 boolean_t b_new_pes );
59 /*****************************************************************************
61 *****************************************************************************/
62 void _M( adec_getfunctions )( function_list_t * p_function_list )
64 p_function_list->pf_probe = decoder_Probe;
65 p_function_list->functions.dec.pf_run = decoder_Run;
68 /*****************************************************************************
69 * Build configuration tree.
70 *****************************************************************************/
75 SET_DESCRIPTION( "software AC3 decoder" )
76 ADD_CAPABILITY( DECODER, 50 )
80 _M( adec_getfunctions )( &p_module->p_functions->dec );
83 MODULE_DEACTIVATE_START
84 MODULE_DEACTIVATE_STOP
87 /*****************************************************************************
88 * decoder_Probe: probe the decoder and return score
89 *****************************************************************************
90 * Tries to launch a decoder and return score so that the interface is able
92 *****************************************************************************/
93 static int decoder_Probe( probedata_t *p_data )
95 return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
99 /*****************************************************************************
100 * InitThread: initialize data before entering main loop
101 *****************************************************************************/
102 static int InitThread( ac3dec_thread_t * p_ac3thread )
107 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
108 p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
111 * Choose the best downmix module
113 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
114 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX,
115 main_GetPszVariable( DOWNMIX_METHOD_VAR, NULL ),
118 if( DOWNMIX.p_module == NULL )
120 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
121 free( p_ac3thread->ac3_decoder );
125 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
126 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
127 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
128 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
129 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
130 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
131 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
132 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
137 * Choose the best IMDCT module
139 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
141 #define IMDCT p_ac3thread->ac3_decoder->imdct
142 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT,
143 main_GetPszVariable( IMDCT_METHOD_VAR, NULL ),
146 if( IMDCT->p_module == NULL )
148 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
149 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
150 free( p_ac3thread->ac3_decoder->imdct );
151 free( p_ac3thread->ac3_decoder );
155 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
156 IMDCT->pf_imdct_init = F.pf_imdct_init;
157 IMDCT->pf_imdct_256 = F.pf_imdct_256;
158 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
159 IMDCT->pf_imdct_512 = F.pf_imdct_512;
160 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
163 /* Initialize the ac3 decoder structures */
164 #define p_dec p_ac3thread->ac3_decoder
165 #if defined( __MINGW32__ )
166 p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
167 p_dec->samples = (float *)
168 (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
170 p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
174 IMDCT->buf = memalign( 16, N/4 * sizeof(complex_t) );
175 IMDCT->delay = memalign( 16, 6 * 256 * sizeof(float) );
176 IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
177 IMDCT->xcos1 = memalign( 16, N/4 * sizeof(float) );
178 IMDCT->xsin1 = memalign( 16, N/4 * sizeof(float) );
179 IMDCT->xcos2 = memalign( 16, N/8 * sizeof(float) );
180 IMDCT->xsin2 = memalign( 16, N/8 * sizeof(float) );
181 IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
182 IMDCT->w_1 = memalign( 16, 1 * sizeof(complex_t) );
183 IMDCT->w_2 = memalign( 16, 2 * sizeof(complex_t) );
184 IMDCT->w_4 = memalign( 16, 4 * sizeof(complex_t) );
185 IMDCT->w_8 = memalign( 16, 8 * sizeof(complex_t) );
186 IMDCT->w_16 = memalign( 16, 16 * sizeof(complex_t) );
187 IMDCT->w_32 = memalign( 16, 32 * sizeof(complex_t) );
188 IMDCT->w_64 = memalign( 16, 64 * sizeof(complex_t) );
190 ac3_init( p_ac3thread->ac3_decoder );
193 * Initialize the output properties
195 p_ac3thread->p_aout_fifo = NULL;
197 intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized",
203 p_ac3thread->p_config->pf_init_bit_stream(
204 &p_ac3thread->ac3_decoder->bit_stream,
205 p_ac3thread->p_config->p_decoder_fifo,
206 BitstreamCallback, (void *) p_ac3thread );
208 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
213 /*****************************************************************************
214 * decoder_Run: this function is called just after the thread is created
215 *****************************************************************************/
216 static int decoder_Run ( decoder_config_t * p_config )
218 ac3dec_thread_t * p_ac3thread;
221 intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
223 /* Allocate the memory needed to store the thread's structure */
224 p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
226 if( p_ac3thread == NULL )
228 intf_ErrMsg ( "ac3_adec error: not enough memory "
229 "for decoder_Run() to allocate p_ac3thread" );
230 DecoderError( p_config->p_decoder_fifo );
235 * Initialize the thread properties
237 p_ac3thread->p_config = p_config;
238 if( InitThread( p_ac3thread ) )
240 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
241 DecoderError( p_config->p_decoder_fifo );
247 p_ac3thread->sync_ptr = 0;
249 /* ac3 decoder thread's main loop */
250 /* FIXME : do we have enough room to store the decoded frames ?? */
251 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
254 ac3_sync_info_t sync_info;
259 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
260 } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
261 && (!p_ac3thread->p_fifo->b_error));
263 ptr = p_ac3thread->sync_ptr;
265 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
266 && (!p_ac3thread->p_fifo->b_error))
268 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
271 /* we are in sync now */
275 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
281 /* Creating the audio output fifo if not created yet */
282 if (p_ac3thread->p_aout_fifo == NULL ) {
283 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO,
284 2, sync_info.sample_rate, 0, AC3DEC_FRAME_SIZE, NULL );
285 if ( p_ac3thread->p_aout_fifo == NULL )
294 free( IMDCT->xcos_sin_sse );
295 free( IMDCT->xsin2 );
296 free( IMDCT->xcos2 );
297 free( IMDCT->xsin1 );
298 free( IMDCT->xcos1 );
299 free( IMDCT->delay1 );
300 free( IMDCT->delay );
304 #if defined( __MINGW32__ )
305 free( p_ac3thread->ac3_decoder->samples_back );
307 free( p_ac3thread->ac3_decoder->samples );
310 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
311 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
313 free( p_ac3thread->ac3_decoder->imdct );
314 free( p_ac3thread->ac3_decoder );
320 CurrentPTS( &p_ac3thread->ac3_decoder->bit_stream,
321 &p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame],
323 if( !p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->l_end_frame] )
325 p_ac3thread->p_aout_fifo->date[
326 p_ac3thread->p_aout_fifo->l_end_frame] =
330 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
331 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
333 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
339 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
340 p_ac3thread->p_aout_fifo->l_end_frame =
341 (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
342 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
343 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
346 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
349 /* If b_error is set, the ac3 decoder thread enters the error loop */
350 if (p_ac3thread->p_fifo->b_error)
352 DecoderError( p_ac3thread->p_fifo );
355 /* End of the ac3 decoder thread */
356 EndThread (p_ac3thread);
364 /*****************************************************************************
365 * EndThread : ac3 decoder thread destruction
366 *****************************************************************************/
367 static void EndThread (ac3dec_thread_t * p_ac3thread)
369 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
371 /* If the audio output fifo was created, we destroy it */
372 if (p_ac3thread->p_aout_fifo != NULL)
374 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
376 /* Make sure the output thread leaves the NextFrame() function */
377 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
378 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
379 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
382 /* Free allocated structures */
383 #define IMDCT p_ac3thread->ac3_decoder->imdct
391 free( IMDCT->xcos_sin_sse );
392 free( IMDCT->xsin2 );
393 free( IMDCT->xcos2 );
394 free( IMDCT->xsin1 );
395 free( IMDCT->xcos1 );
396 free( IMDCT->delay1 );
397 free( IMDCT->delay );
401 #if defined( __MINGW32__ )
402 free( p_ac3thread->ac3_decoder->samples_back );
404 free( p_ac3thread->ac3_decoder->samples );
407 /* Unlock the modules */
408 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
409 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
411 /* Free what's left of the decoder */
412 free( p_ac3thread->ac3_decoder->imdct );
413 free( p_ac3thread->ac3_decoder );
415 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
418 /*****************************************************************************
419 * BitstreamCallback: Import parameters from the new data/PES packet
420 *****************************************************************************
421 * This function is called by input's NextDataPacket.
422 *****************************************************************************/
423 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
427 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
433 ptr = *(p_bit_stream->p_byte + 1);
435 ptr |= *(p_bit_stream->p_byte + 2);
436 p_ac3thread->sync_ptr = ptr;
437 p_bit_stream->p_byte += 3;