1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: ac3_adec.c,v 1.3 2001/11/15 17:39:12 sam 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 return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
121 /*****************************************************************************
122 * ac3_adec_Run: this function is called just after the thread is created
123 *****************************************************************************/
124 static int ac3_adec_Run ( decoder_config_t * p_config )
126 ac3dec_thread_t * p_ac3thread;
129 intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
131 /* Allocate the memory needed to store the thread's structure */
132 p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
134 if( p_ac3thread == NULL )
136 intf_ErrMsg ( "ac3_adec error: not enough memory "
137 "for ac3_adec_Run() to allocate p_ac3thread" );
142 * Initialize the thread properties
144 p_ac3thread->p_config = p_config;
145 if( ac3_adec_Init( p_ac3thread ) )
147 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
153 p_ac3thread->sync_ptr = 0;
155 /* ac3 decoder thread's main loop */
156 /* FIXME : do we have enough room to store the decoded frames ?? */
157 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
160 ac3_sync_info_t sync_info;
165 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
166 } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
167 && (!p_ac3thread->p_fifo->b_error));
169 ptr = p_ac3thread->sync_ptr;
171 while(ptr-- && (!p_ac3thread->p_fifo->b_die)
172 && (!p_ac3thread->p_fifo->b_error))
174 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
177 /* we are in sync now */
181 if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
183 p_ac3thread->p_aout_fifo->date[
184 p_ac3thread->p_aout_fifo->l_end_frame] =
185 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
186 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
188 p_ac3thread->p_aout_fifo->date[
189 p_ac3thread->p_aout_fifo->l_end_frame] =
193 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
199 p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
201 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
202 (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
204 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
210 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
211 p_ac3thread->p_aout_fifo->l_end_frame =
212 (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
213 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
214 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
217 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
220 /* If b_error is set, the ac3 decoder thread enters the error loop */
221 if (p_ac3thread->p_fifo->b_error)
223 ac3_adec_ErrorThread (p_ac3thread);
226 /* End of the ac3 decoder thread */
227 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 );
259 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
260 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
261 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
262 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
263 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
264 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
265 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
266 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
271 * Choose the best IMDCT module
273 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
275 #define IMDCT p_ac3thread->ac3_decoder->imdct
276 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
278 if( IMDCT->p_module == NULL )
280 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
281 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
282 free( p_ac3thread->ac3_decoder->imdct );
283 free( p_ac3thread->ac3_decoder );
287 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
288 IMDCT->pf_imdct_init = F.pf_imdct_init;
289 IMDCT->pf_imdct_256 = F.pf_imdct_256;
290 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
291 IMDCT->pf_imdct_512 = F.pf_imdct_512;
292 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
295 /* Initialize the ac3 decoder structures */
296 #define p_dec p_ac3thread->ac3_decoder
297 #if defined( __MINGW32__ )
298 p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
299 p_dec->samples = (float *)
300 (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
302 p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
306 IMDCT->buf = memalign( 16, N/4 * sizeof(complex_t) );
307 IMDCT->delay = memalign( 16, 6 * 256 * sizeof(float) );
308 IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
309 IMDCT->xcos1 = memalign( 16, N/4 * sizeof(float) );
310 IMDCT->xsin1 = memalign( 16, N/4 * sizeof(float) );
311 IMDCT->xcos2 = memalign( 16, N/8 * sizeof(float) );
312 IMDCT->xsin2 = memalign( 16, N/8 * sizeof(float) );
313 IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
314 IMDCT->w_1 = memalign( 16, 1 * sizeof(complex_t) );
315 IMDCT->w_2 = memalign( 16, 2 * sizeof(complex_t) );
316 IMDCT->w_4 = memalign( 16, 4 * sizeof(complex_t) );
317 IMDCT->w_8 = memalign( 16, 8 * sizeof(complex_t) );
318 IMDCT->w_16 = memalign( 16, 16 * sizeof(complex_t) );
319 IMDCT->w_32 = memalign( 16, 32 * sizeof(complex_t) );
320 IMDCT->w_64 = memalign( 16, 64 * sizeof(complex_t) );
322 ac3_init( p_ac3thread->ac3_decoder );
325 * Initialize the output properties
327 p_ac3thread->p_aout_fifo = NULL;
329 intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized",
335 p_ac3thread->p_config->pf_init_bit_stream(
336 &p_ac3thread->ac3_decoder->bit_stream,
337 p_ac3thread->p_config->p_decoder_fifo,
338 BitstreamCallback, (void *) p_ac3thread );
340 /* Creating the audio output fifo */
341 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
342 AC3DEC_FRAME_SIZE, NULL );
343 if ( p_ac3thread->p_aout_fifo == NULL )
352 free( IMDCT->xcos_sin_sse );
353 free( IMDCT->xsin2 );
354 free( IMDCT->xcos2 );
355 free( IMDCT->xsin1 );
356 free( IMDCT->xcos1 );
357 free( IMDCT->delay1 );
358 free( IMDCT->delay );
362 #if defined( __MINGW32__ )
363 free( p_ac3thread->ac3_decoder->samples_back );
365 free( p_ac3thread->ac3_decoder->samples );
368 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
369 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
371 free( p_ac3thread->ac3_decoder->imdct );
372 free( p_ac3thread->ac3_decoder );
377 intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
383 /*****************************************************************************
384 * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
385 *****************************************************************************/
386 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
388 /* We take the lock, because we are going to read/write the start/end
389 * indexes of the decoder fifo */
390 vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
392 /* Wait until a `die' order is sent */
393 while (!p_ac3thread->p_fifo->b_die)
395 /* Trash all received PES packets */
396 while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
398 p_ac3thread->p_fifo->pf_delete_pes(
399 p_ac3thread->p_fifo->p_packets_mgt,
400 DECODER_FIFO_START(*p_ac3thread->p_fifo));
401 DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
404 /* Waiting for the input thread to put new PES packets in the fifo */
405 vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
406 &p_ac3thread->p_fifo->data_lock);
409 /* We can release the lock before leaving */
410 vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
413 /*****************************************************************************
414 * ac3_adec_EndThread : ac3 decoder thread destruction
415 *****************************************************************************/
416 static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
418 intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
420 /* If the audio output fifo was created, we destroy it */
421 if (p_ac3thread->p_aout_fifo != NULL)
423 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
425 /* Make sure the output thread leaves the NextFrame() function */
426 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
427 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
428 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
431 /* Free allocated structures */
432 #define IMDCT p_ac3thread->ac3_decoder->imdct
440 free( IMDCT->xcos_sin_sse );
441 free( IMDCT->xsin2 );
442 free( IMDCT->xcos2 );
443 free( IMDCT->xsin1 );
444 free( IMDCT->xcos1 );
445 free( IMDCT->delay1 );
446 free( IMDCT->delay );
450 #if defined( __MINGW32__ )
451 free( p_ac3thread->ac3_decoder->samples_back );
453 free( p_ac3thread->ac3_decoder->samples );
456 /* Unlock the modules */
457 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
458 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
460 /* Free what's left of the decoder */
461 free( p_ac3thread->ac3_decoder->imdct );
462 free( p_ac3thread->ac3_decoder );
464 intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
467 /*****************************************************************************
468 * BitstreamCallback: Import parameters from the new data/PES packet
469 *****************************************************************************
470 * This function is called by input's NextDataPacket.
471 *****************************************************************************/
472 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
476 ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
482 ptr = *(p_bit_stream->p_byte + 1);
484 ptr |= *(p_bit_stream->p_byte + 2);
485 p_ac3thread->sync_ptr = ptr;
486 p_bit_stream->p_byte += 3;