1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: ac3_adec.c,v 1.1 2001/11/13 12:09:17 henri 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 *****************************************************************************/
27 * - vérifier l'état de la fifo de sortie avant d'y stocker les samples
29 * - vlc_cond_signal() / vlc_cond_wait()
32 #define MODULE_NAME ac3_adec
33 #include "modules_inner.h"
35 /*****************************************************************************
37 *****************************************************************************/
41 #include <unistd.h> /* getpid() */
44 #include <stdlib.h> /* malloc(), free() */
45 #include <string.h> /* memset() */
51 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
53 #include "audio_output.h"
56 #include "modules_export.h"
58 #include "stream_control.h"
59 #include "input_ext-dec.h"
60 #include "input_ext-intf.h" /* MPEG?_AUDIO_ES */
62 #include "ac3_imdct.h"
63 #include "ac3_downmix.h"
64 #include "ac3_decoder.h"
67 #define AC3DEC_FRAME_SIZE (2*1536)
69 /*****************************************************************************
71 *****************************************************************************/
72 static int ac3_adec_Probe ( probedata_t * );
73 static int ac3_adec_Run ( decoder_config_t * );
74 static int ac3_adec_Init (ac3dec_thread_t * p_adec);
75 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_adec);
76 static void ac3_adec_EndThread (ac3dec_thread_t * p_adec);
77 static void BitstreamCallback ( bit_stream_t *p_bit_stream,
78 boolean_t b_new_pes );
80 /*****************************************************************************
82 *****************************************************************************/
83 void _M( adec_getfunctions )( function_list_t * p_function_list )
85 p_function_list->pf_probe = ac3_adec_Probe;
86 p_function_list->functions.dec.pf_RunThread = ac3_adec_Run;
89 /*****************************************************************************
90 * Build configuration tree.
91 *****************************************************************************/
93 ADD_WINDOW( "Configuration for ac3 decoder module" )
94 ADD_COMMENT( "Nothing to configure" )
98 p_module->i_capabilities = MODULE_CAPABILITY_DEC;
99 p_module->psz_longname = "Ac3 sofware decoder";
102 MODULE_ACTIVATE_START
103 _M( adec_getfunctions )( &p_module->p_functions->dec );
106 MODULE_DEACTIVATE_START
107 MODULE_DEACTIVATE_STOP
110 /*****************************************************************************
111 * ac3_adec_Probe: probe the decoder and return score
112 *****************************************************************************
113 * Tries to launch a decoder and return score so that the interface is able
115 *****************************************************************************/
116 static int ac3_adec_Probe( probedata_t *p_data )
118 if( p_data->i_type == AC3_AUDIO_ES )
124 /*****************************************************************************
125 * ac3_adec_Run: this function is called just after the thread is created
126 *****************************************************************************/
127 static int ac3_adec_Run ( decoder_config_t * p_config )
129 ac3dec_thread_t * p_ac3thread;
132 intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
134 /* Allocate the memory needed to store the thread's structure */
135 p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
137 if(p_ac3thread == NULL)
139 intf_ErrMsg ( "ac3dec error: not enough memory "
140 "for ac3_adec_Run() to allocate p_ac3thread" );
145 * Initialize the thread properties
147 p_ac3thread->p_config = p_config;
148 if( ac3_adec_Init( p_ac3thread ) )
150 intf_ErrMsg( "ac3_adec error : could not initialize thread" );
155 p_ac3thread->sync_ptr = 0;
157 /* ac3 decoder thread's main loop */
158 /* FIXME : do we have enough room to store the decoded frames ?? */
159 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
162 ac3_sync_info_t sync_info;
167 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
168 } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
169 && (!p_ac3thread->p_fifo->b_error));
171 ptr = p_ac3thread->sync_ptr;
173 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
174 && (!p_ac3thread->p_fifo->b_error))
176 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
179 /* we are in sync now */
183 if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
185 p_ac3thread->p_aout_fifo->date[
186 p_ac3thread->p_aout_fifo->l_end_frame] =
187 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
188 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
190 p_ac3thread->p_aout_fifo->date[
191 p_ac3thread->p_aout_fifo->l_end_frame] =
195 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
201 p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
203 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
204 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
206 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
212 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
213 p_ac3thread->p_aout_fifo->l_end_frame =
214 (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
215 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
216 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
219 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
222 /* If b_error is set, the ac3 decoder thread enters the error loop */
223 if (p_ac3thread->p_fifo->b_error)
225 ac3_adec_ErrorThread (p_ac3thread);
228 /* End of the ac3 decoder thread */
229 ac3_adec_EndThread (p_ac3thread);
235 /*****************************************************************************
236 * ac3_adec_Init: initialize data before entering main loop
237 *****************************************************************************/
238 static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
243 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
244 p_ac3thread->ac3_decoder = memalign(16, sizeof(ac3dec_t));
247 * Choose the best downmix module
249 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
250 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
252 if( DOWNMIX.p_module == NULL )
254 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
255 free( p_ac3thread->ac3_decoder );
260 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
261 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
262 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
263 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
264 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
265 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
266 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
267 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
272 * Choose the best IMDCT module
274 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
276 #define IMDCT p_ac3thread->ac3_decoder->imdct
277 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
279 if( IMDCT->p_module == NULL )
281 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
282 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
283 free( p_ac3thread->ac3_decoder->imdct );
284 free( p_ac3thread->ac3_decoder );
289 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
290 IMDCT->pf_imdct_init = F.pf_imdct_init;
291 IMDCT->pf_imdct_256 = F.pf_imdct_256;
292 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
293 IMDCT->pf_imdct_512 = F.pf_imdct_512;
294 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
298 /* Initialize the ac3 decoder structures */
299 #if defined( __MINGW32__ )
300 p_ac3thread->ac3_decoder->samples_back = memalign(16, 6 * 256 *
302 p_ac3thread->ac3_decoder->samples = (float *) (((unsigned long)
303 p_ac3thread->ac3_decoder->samples_back+15) & ~0xFUL);
305 p_ac3thread->ac3_decoder->samples = memalign(16, 6 * 256 * sizeof(float));
307 p_ac3thread->ac3_decoder->imdct->buf = memalign(16, N/4 * sizeof(complex_t));
308 p_ac3thread->ac3_decoder->imdct->delay = memalign(16, 6 * 256 * sizeof(float));
309 p_ac3thread->ac3_decoder->imdct->delay1 = memalign(16, 6 * 256 * sizeof(float));
310 p_ac3thread->ac3_decoder->imdct->xcos1 = memalign(16, N/4 * sizeof(float));
311 p_ac3thread->ac3_decoder->imdct->xsin1 = memalign(16, N/4 * sizeof(float));
312 p_ac3thread->ac3_decoder->imdct->xcos2 = memalign(16, N/8 * sizeof(float));
313 p_ac3thread->ac3_decoder->imdct->xsin2 = memalign(16, N/8 * sizeof(float));
314 p_ac3thread->ac3_decoder->imdct->xcos_sin_sse = memalign(16, 128 * 4 * sizeof(float));
315 p_ac3thread->ac3_decoder->imdct->w_2 = memalign(16, 2 * sizeof(complex_t));
316 p_ac3thread->ac3_decoder->imdct->w_4 = memalign(16, 4 * sizeof(complex_t));
317 p_ac3thread->ac3_decoder->imdct->w_8 = memalign(16, 8 * sizeof(complex_t));
318 p_ac3thread->ac3_decoder->imdct->w_16 = memalign(16, 16 * sizeof(complex_t));
319 p_ac3thread->ac3_decoder->imdct->w_32 = memalign(16, 32 * sizeof(complex_t));
320 p_ac3thread->ac3_decoder->imdct->w_64 = memalign(16, 64 * sizeof(complex_t));
321 p_ac3thread->ac3_decoder->imdct->w_1 = memalign(16, sizeof(complex_t));
323 ac3_init (p_ac3thread->ac3_decoder);
326 * Initialize the output properties
328 p_ac3thread->p_aout_fifo = NULL;
330 intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized",
336 p_ac3thread->p_config->pf_init_bit_stream(
337 &p_ac3thread->ac3_decoder->bit_stream,
338 p_ac3thread->p_config->p_decoder_fifo,
339 BitstreamCallback, (void *) p_ac3thread );
341 /* Creating the audio output fifo */
342 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
343 AC3DEC_FRAME_SIZE, NULL );
344 if ( p_ac3thread->p_aout_fifo == NULL )
349 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
355 /*****************************************************************************
356 * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
357 *****************************************************************************/
358 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
360 /* We take the lock, because we are going to read/write the start/end
361 * indexes of the decoder fifo */
362 vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
364 /* Wait until a `die' order is sent */
365 while (!p_ac3thread->p_fifo->b_die)
367 /* Trash all received PES packets */
368 while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
370 p_ac3thread->p_fifo->pf_delete_pes(
371 p_ac3thread->p_fifo->p_packets_mgt,
372 DECODER_FIFO_START(*p_ac3thread->p_fifo));
373 DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
376 /* Waiting for the input thread to put new PES packets in the fifo */
377 vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
378 &p_ac3thread->p_fifo->data_lock);
381 /* We can release the lock before leaving */
382 vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
385 /*****************************************************************************
386 * ac3_adec_EndThread : ac3 decoder thread destruction
387 *****************************************************************************/
388 static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
390 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
392 /* If the audio output fifo was created, we destroy it */
393 if (p_ac3thread->p_aout_fifo != NULL)
395 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
397 /* Make sure the output thread leaves the NextFrame() function */
398 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
399 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
400 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
403 /* Unlock the modules */
404 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
405 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
407 /* Destroy descriptor */
408 free( p_ac3thread->ac3_decoder->imdct->w_1 );
409 free( p_ac3thread->ac3_decoder->imdct->w_64 );
410 free( p_ac3thread->ac3_decoder->imdct->w_32 );
411 free( p_ac3thread->ac3_decoder->imdct->w_16 );
412 free( p_ac3thread->ac3_decoder->imdct->w_8 );
413 free( p_ac3thread->ac3_decoder->imdct->w_4 );
414 free( p_ac3thread->ac3_decoder->imdct->w_2 );
415 free( p_ac3thread->ac3_decoder->imdct->xcos_sin_sse );
416 free( p_ac3thread->ac3_decoder->imdct->xsin2 );
417 free( p_ac3thread->ac3_decoder->imdct->xcos2 );
418 free( p_ac3thread->ac3_decoder->imdct->xsin1 );
419 free( p_ac3thread->ac3_decoder->imdct->xcos1 );
420 free( p_ac3thread->ac3_decoder->imdct->delay1 );
421 free( p_ac3thread->ac3_decoder->imdct->delay );
422 free( p_ac3thread->ac3_decoder->imdct->buf );
423 #if defined( __MINGW32__ )
424 free( p_ac3thread->ac3_decoder->samples_back );
426 free( p_ac3thread->ac3_decoder->samples );
428 free( p_ac3thread->ac3_decoder->imdct );
429 free( p_ac3thread->ac3_decoder );
430 free( p_ac3thread->p_config );
433 intf_DbgMsg ("ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread);
436 /*****************************************************************************
437 * BitstreamCallback: Import parameters from the new data/PES packet
438 *****************************************************************************
439 * This function is called by input's NextDataPacket.
440 *****************************************************************************/
441 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
445 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
451 ptr = *(p_bit_stream->p_byte + 1);
453 ptr |= *(p_bit_stream->p_byte + 2);
454 p_ac3thread->sync_ptr = ptr;
455 p_bit_stream->p_byte += 3;