1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_adec.c,v 1.9 2001/12/27 01:49:34 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 #define MODULE_NAME ac3_adec
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
33 #include <unistd.h> /* getpid() */
36 #include <stdlib.h> /* malloc(), free() */
37 #include <string.h> /* memset() */
40 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
44 #include "audio_output.h"
46 #include "stream_control.h"
47 #include "input_ext-dec.h"
48 #include "input_ext-intf.h" /* MPEG?_AUDIO_ES */
51 #include "modules_export.h"
53 #include "ac3_imdct.h"
54 #include "ac3_downmix.h"
55 #include "ac3_decoder.h"
58 #define AC3DEC_FRAME_SIZE (2*1536)
60 /*****************************************************************************
62 *****************************************************************************/
63 static int ac3_adec_Probe ( probedata_t * );
64 static int ac3_adec_Run ( decoder_config_t * );
65 static int ac3_adec_Init (ac3dec_thread_t * p_adec);
66 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_adec);
67 static void ac3_adec_EndThread (ac3dec_thread_t * p_adec);
68 static void BitstreamCallback ( bit_stream_t *p_bit_stream,
69 boolean_t b_new_pes );
71 /*****************************************************************************
73 *****************************************************************************/
74 void _M( adec_getfunctions )( function_list_t * p_function_list )
76 p_function_list->pf_probe = ac3_adec_Probe;
77 p_function_list->functions.dec.pf_run = ac3_adec_Run;
80 /*****************************************************************************
81 * Build configuration tree.
82 *****************************************************************************/
84 ADD_WINDOW( "Configuration for AC3 decoder module" )
85 ADD_COMMENT( "Nothing to configure" )
89 p_module->i_capabilities = MODULE_CAPABILITY_DEC;
90 p_module->psz_longname = "AC3 sofware decoder";
94 _M( adec_getfunctions )( &p_module->p_functions->dec );
97 MODULE_DEACTIVATE_START
98 MODULE_DEACTIVATE_STOP
101 /*****************************************************************************
102 * ac3_adec_Probe: probe the decoder and return score
103 *****************************************************************************
104 * Tries to launch a decoder and return score so that the interface is able
106 *****************************************************************************/
107 static int ac3_adec_Probe( probedata_t *p_data )
109 return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
112 /*****************************************************************************
113 * ac3_adec_Run: this function is called just after the thread is created
114 *****************************************************************************/
115 static int ac3_adec_Run ( decoder_config_t * p_config )
117 ac3dec_thread_t * p_ac3thread;
120 intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
122 /* Allocate the memory needed to store the thread's structure */
123 p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
125 if( p_ac3thread == NULL )
127 intf_ErrMsg ( "ac3_adec error: not enough memory "
128 "for ac3_adec_Run() to allocate p_ac3thread" );
133 * Initialize the thread properties
135 p_ac3thread->p_config = p_config;
136 if( ac3_adec_Init( p_ac3thread ) )
138 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
144 p_ac3thread->sync_ptr = 0;
146 /* ac3 decoder thread's main loop */
147 /* FIXME : do we have enough room to store the decoded frames ?? */
148 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
151 ac3_sync_info_t sync_info;
156 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
157 } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
158 && (!p_ac3thread->p_fifo->b_error));
160 ptr = p_ac3thread->sync_ptr;
162 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
163 && (!p_ac3thread->p_fifo->b_error))
165 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
168 /* we are in sync now */
172 if (p_ac3thread->p_fifo->p_first->i_pts)
174 p_ac3thread->p_aout_fifo->date[
175 p_ac3thread->p_aout_fifo->l_end_frame] =
176 p_ac3thread->p_fifo->p_first->i_pts;
177 p_ac3thread->p_fifo->p_first->i_pts = 0;
179 p_ac3thread->p_aout_fifo->date[
180 p_ac3thread->p_aout_fifo->l_end_frame] =
184 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
190 p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
192 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
193 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
195 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
201 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
202 p_ac3thread->p_aout_fifo->l_end_frame =
203 (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
204 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
205 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
208 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
211 /* If b_error is set, the ac3 decoder thread enters the error loop */
212 if (p_ac3thread->p_fifo->b_error)
214 ac3_adec_ErrorThread (p_ac3thread);
217 /* End of the ac3 decoder thread */
218 ac3_adec_EndThread (p_ac3thread);
226 /*****************************************************************************
227 * ac3_adec_Init: initialize data before entering main loop
228 *****************************************************************************/
229 static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
234 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
235 p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
238 * Choose the best downmix module
240 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
241 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
243 if( DOWNMIX.p_module == NULL )
245 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
246 free( p_ac3thread->ac3_decoder );
250 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
251 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
252 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
253 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
254 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
255 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
256 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
257 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
262 * Choose the best IMDCT module
264 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
266 #define IMDCT p_ac3thread->ac3_decoder->imdct
267 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
269 if( IMDCT->p_module == NULL )
271 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
272 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
273 free( p_ac3thread->ac3_decoder->imdct );
274 free( p_ac3thread->ac3_decoder );
278 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
279 IMDCT->pf_imdct_init = F.pf_imdct_init;
280 IMDCT->pf_imdct_256 = F.pf_imdct_256;
281 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
282 IMDCT->pf_imdct_512 = F.pf_imdct_512;
283 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
286 /* Initialize the ac3 decoder structures */
287 #define p_dec p_ac3thread->ac3_decoder
288 #if defined( __MINGW32__ )
289 p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
290 p_dec->samples = (float *)
291 (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
293 p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
297 IMDCT->buf = memalign( 16, N/4 * sizeof(complex_t) );
298 IMDCT->delay = memalign( 16, 6 * 256 * sizeof(float) );
299 IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
300 IMDCT->xcos1 = memalign( 16, N/4 * sizeof(float) );
301 IMDCT->xsin1 = memalign( 16, N/4 * sizeof(float) );
302 IMDCT->xcos2 = memalign( 16, N/8 * sizeof(float) );
303 IMDCT->xsin2 = memalign( 16, N/8 * sizeof(float) );
304 IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
305 IMDCT->w_1 = memalign( 16, 1 * sizeof(complex_t) );
306 IMDCT->w_2 = memalign( 16, 2 * sizeof(complex_t) );
307 IMDCT->w_4 = memalign( 16, 4 * sizeof(complex_t) );
308 IMDCT->w_8 = memalign( 16, 8 * sizeof(complex_t) );
309 IMDCT->w_16 = memalign( 16, 16 * sizeof(complex_t) );
310 IMDCT->w_32 = memalign( 16, 32 * sizeof(complex_t) );
311 IMDCT->w_64 = memalign( 16, 64 * sizeof(complex_t) );
313 ac3_init( p_ac3thread->ac3_decoder );
316 * Initialize the output properties
318 p_ac3thread->p_aout_fifo = NULL;
320 intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized",
326 p_ac3thread->p_config->pf_init_bit_stream(
327 &p_ac3thread->ac3_decoder->bit_stream,
328 p_ac3thread->p_config->p_decoder_fifo,
329 BitstreamCallback, (void *) p_ac3thread );
331 /* Creating the audio output fifo */
332 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
333 AC3DEC_FRAME_SIZE, NULL );
334 if ( p_ac3thread->p_aout_fifo == NULL )
343 free( IMDCT->xcos_sin_sse );
344 free( IMDCT->xsin2 );
345 free( IMDCT->xcos2 );
346 free( IMDCT->xsin1 );
347 free( IMDCT->xcos1 );
348 free( IMDCT->delay1 );
349 free( IMDCT->delay );
353 #if defined( __MINGW32__ )
354 free( p_ac3thread->ac3_decoder->samples_back );
356 free( p_ac3thread->ac3_decoder->samples );
359 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
360 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
362 free( p_ac3thread->ac3_decoder->imdct );
363 free( p_ac3thread->ac3_decoder );
368 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
374 /*****************************************************************************
375 * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
376 *****************************************************************************/
377 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
379 /* We take the lock, because we are going to read/write the start/end
380 * indexes of the decoder fifo */
381 vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
383 /* Wait until a `die' order is sent */
384 while (!p_ac3thread->p_fifo->b_die)
386 /* Trash all received PES packets */
387 p_ac3thread->p_fifo->pf_delete_pes(
388 p_ac3thread->p_fifo->p_packets_mgt,
389 p_ac3thread->p_fifo->p_first );
391 /* Waiting for the input thread to put new PES packets in the fifo */
392 vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
393 &p_ac3thread->p_fifo->data_lock);
396 /* We can release the lock before leaving */
397 vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
400 /*****************************************************************************
401 * ac3_adec_EndThread : ac3 decoder thread destruction
402 *****************************************************************************/
403 static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
405 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
407 /* If the audio output fifo was created, we destroy it */
408 if (p_ac3thread->p_aout_fifo != NULL)
410 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
412 /* Make sure the output thread leaves the NextFrame() function */
413 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
414 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
415 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
418 /* Free allocated structures */
419 #define IMDCT p_ac3thread->ac3_decoder->imdct
427 free( IMDCT->xcos_sin_sse );
428 free( IMDCT->xsin2 );
429 free( IMDCT->xcos2 );
430 free( IMDCT->xsin1 );
431 free( IMDCT->xcos1 );
432 free( IMDCT->delay1 );
433 free( IMDCT->delay );
437 #if defined( __MINGW32__ )
438 free( p_ac3thread->ac3_decoder->samples_back );
440 free( p_ac3thread->ac3_decoder->samples );
443 /* Unlock the modules */
444 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
445 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
447 /* Free what's left of the decoder */
448 free( p_ac3thread->ac3_decoder->imdct );
449 free( p_ac3thread->ac3_decoder );
451 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
454 /*****************************************************************************
455 * BitstreamCallback: Import parameters from the new data/PES packet
456 *****************************************************************************
457 * This function is called by input's NextDataPacket.
458 *****************************************************************************/
459 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
463 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
469 ptr = *(p_bit_stream->p_byte + 1);
471 ptr |= *(p_bit_stream->p_byte + 2);
472 p_ac3thread->sync_ptr = ptr;
473 p_bit_stream->p_byte += 3;