1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_adec.c,v 1.5.2.2 2001/12/31 01:21:44 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() */
41 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
45 #include "audio_output.h"
48 #include "modules_export.h"
50 #include "stream_control.h"
51 #include "input_ext-dec.h"
52 #include "input_ext-intf.h" /* MPEG?_AUDIO_ES */
54 #include "ac3_imdct.h"
55 #include "ac3_downmix.h"
56 #include "ac3_decoder.h"
59 #define AC3DEC_FRAME_SIZE (2*1536)
61 /*****************************************************************************
63 *****************************************************************************/
64 static int decoder_Probe ( probedata_t * );
65 static int decoder_Run ( decoder_config_t * );
66 static int InitThread ( ac3dec_thread_t * p_adec );
67 static void 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 = decoder_Probe;
77 p_function_list->functions.dec.pf_run = decoder_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 * decoder_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 decoder_Probe( probedata_t *p_data )
109 return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
112 /*****************************************************************************
113 * decoder_Run: this function is called just after the thread is created
114 *****************************************************************************/
115 static int decoder_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 decoder_Run() to allocate p_ac3thread" );
129 DecoderError( p_config->p_decoder_fifo );
134 * Initialize the thread properties
136 p_ac3thread->p_config = p_config;
137 if( InitThread( p_ac3thread ) )
139 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
140 DecoderError( p_config->p_decoder_fifo );
146 p_ac3thread->sync_ptr = 0;
148 /* ac3 decoder thread's main loop */
149 /* FIXME : do we have enough room to store the decoded frames ?? */
150 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
153 ac3_sync_info_t sync_info;
158 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
159 } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
160 && (!p_ac3thread->p_fifo->b_error));
162 ptr = p_ac3thread->sync_ptr;
164 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
165 && (!p_ac3thread->p_fifo->b_error))
167 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
170 /* we are in sync now */
174 if (p_ac3thread->p_fifo->p_first->i_pts)
176 p_ac3thread->p_aout_fifo->date[
177 p_ac3thread->p_aout_fifo->l_end_frame] =
178 p_ac3thread->p_fifo->p_first->i_pts;
179 p_ac3thread->p_fifo->p_first->i_pts = 0;
181 p_ac3thread->p_aout_fifo->date[
182 p_ac3thread->p_aout_fifo->l_end_frame] =
186 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
192 p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
194 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
195 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
197 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
203 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
204 p_ac3thread->p_aout_fifo->l_end_frame =
205 (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
206 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
207 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
210 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
213 /* If b_error is set, the ac3 decoder thread enters the error loop */
214 if (p_ac3thread->p_fifo->b_error)
216 DecoderError( p_ac3thread->p_fifo );
219 /* End of the ac3 decoder thread */
220 EndThread (p_ac3thread);
228 /*****************************************************************************
229 * InitThread: initialize data before entering main loop
230 *****************************************************************************/
231 static int InitThread( ac3dec_thread_t * p_ac3thread )
236 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
237 p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
240 * Choose the best downmix module
242 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
243 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
245 if( DOWNMIX.p_module == NULL )
247 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
248 free( p_ac3thread->ac3_decoder );
252 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
253 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
254 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
255 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
256 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
257 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
258 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
259 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
264 * Choose the best IMDCT module
266 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
268 #define IMDCT p_ac3thread->ac3_decoder->imdct
269 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
271 if( IMDCT->p_module == NULL )
273 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
274 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
275 free( p_ac3thread->ac3_decoder->imdct );
276 free( p_ac3thread->ac3_decoder );
280 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
281 IMDCT->pf_imdct_init = F.pf_imdct_init;
282 IMDCT->pf_imdct_256 = F.pf_imdct_256;
283 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
284 IMDCT->pf_imdct_512 = F.pf_imdct_512;
285 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
288 /* Initialize the ac3 decoder structures */
289 #define p_dec p_ac3thread->ac3_decoder
290 #if defined( __MINGW32__ )
291 p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
292 p_dec->samples = (float *)
293 (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
295 p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
299 IMDCT->buf = memalign( 16, N/4 * sizeof(complex_t) );
300 IMDCT->delay = memalign( 16, 6 * 256 * sizeof(float) );
301 IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
302 IMDCT->xcos1 = memalign( 16, N/4 * sizeof(float) );
303 IMDCT->xsin1 = memalign( 16, N/4 * sizeof(float) );
304 IMDCT->xcos2 = memalign( 16, N/8 * sizeof(float) );
305 IMDCT->xsin2 = memalign( 16, N/8 * sizeof(float) );
306 IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
307 IMDCT->w_1 = memalign( 16, 1 * sizeof(complex_t) );
308 IMDCT->w_2 = memalign( 16, 2 * sizeof(complex_t) );
309 IMDCT->w_4 = memalign( 16, 4 * sizeof(complex_t) );
310 IMDCT->w_8 = memalign( 16, 8 * sizeof(complex_t) );
311 IMDCT->w_16 = memalign( 16, 16 * sizeof(complex_t) );
312 IMDCT->w_32 = memalign( 16, 32 * sizeof(complex_t) );
313 IMDCT->w_64 = memalign( 16, 64 * sizeof(complex_t) );
315 ac3_init( p_ac3thread->ac3_decoder );
318 * Initialize the output properties
320 p_ac3thread->p_aout_fifo = NULL;
322 intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized",
328 p_ac3thread->p_config->pf_init_bit_stream(
329 &p_ac3thread->ac3_decoder->bit_stream,
330 p_ac3thread->p_config->p_decoder_fifo,
331 BitstreamCallback, (void *) p_ac3thread );
333 /* Creating the audio output fifo */
334 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
335 AC3DEC_FRAME_SIZE, NULL );
336 if ( p_ac3thread->p_aout_fifo == NULL )
345 free( IMDCT->xcos_sin_sse );
346 free( IMDCT->xsin2 );
347 free( IMDCT->xcos2 );
348 free( IMDCT->xsin1 );
349 free( IMDCT->xcos1 );
350 free( IMDCT->delay1 );
351 free( IMDCT->delay );
355 #if defined( __MINGW32__ )
356 free( p_ac3thread->ac3_decoder->samples_back );
358 free( p_ac3thread->ac3_decoder->samples );
361 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
362 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
364 free( p_ac3thread->ac3_decoder->imdct );
365 free( p_ac3thread->ac3_decoder );
370 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
375 /*****************************************************************************
376 * EndThread : ac3 decoder thread destruction
377 *****************************************************************************/
378 static void EndThread (ac3dec_thread_t * p_ac3thread)
380 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
382 /* If the audio output fifo was created, we destroy it */
383 if (p_ac3thread->p_aout_fifo != NULL)
385 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
387 /* Make sure the output thread leaves the NextFrame() function */
388 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
389 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
390 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
393 /* Free allocated structures */
394 #define IMDCT p_ac3thread->ac3_decoder->imdct
402 free( IMDCT->xcos_sin_sse );
403 free( IMDCT->xsin2 );
404 free( IMDCT->xcos2 );
405 free( IMDCT->xsin1 );
406 free( IMDCT->xcos1 );
407 free( IMDCT->delay1 );
408 free( IMDCT->delay );
412 #if defined( __MINGW32__ )
413 free( p_ac3thread->ac3_decoder->samples_back );
415 free( p_ac3thread->ac3_decoder->samples );
418 /* Unlock the modules */
419 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
420 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
422 /* Free what's left of the decoder */
423 free( p_ac3thread->ac3_decoder->imdct );
424 free( p_ac3thread->ac3_decoder );
426 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
429 /*****************************************************************************
430 * BitstreamCallback: Import parameters from the new data/PES packet
431 *****************************************************************************
432 * This function is called by input's NextDataPacket.
433 *****************************************************************************/
434 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
438 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
444 ptr = *(p_bit_stream->p_byte + 1);
446 ptr |= *(p_bit_stream->p_byte + 2);
447 p_ac3thread->sync_ptr = ptr;
448 p_bit_stream->p_byte += 3;