1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_adec.c,v 1.22 2002/02/24 22:06:50 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 *****************************************************************************/
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 ( u8 * );
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->functions.dec.pf_probe = decoder_Probe;
65 p_function_list->functions.dec.pf_run = decoder_Run;
68 /*****************************************************************************
69 * Build configuration tree.
70 *****************************************************************************/
71 /* Variable containing the AC3 downmix method */
72 #define DOWNMIX_METHOD_VAR "ac3_downmix"
73 /* Variable containing the AC3 IMDCT method */
74 #define IMDCT_METHOD_VAR "ac3_imdct"
77 ADD_CATEGORY_HINT( "Misc Options", NULL)
78 ADD_PLUGIN ( DOWNMIX_METHOD_VAR, MODULE_CAPABILITY_DOWNMIX, NULL, NULL,
79 "AC3 downmix method", NULL )
80 ADD_PLUGIN ( IMDCT_METHOD_VAR, MODULE_CAPABILITY_IMDCT, NULL, NULL,
81 "AC3 IMDCT method", NULL )
85 SET_DESCRIPTION( "software AC3 decoder" )
86 ADD_CAPABILITY( DECODER, 50 )
90 _M( adec_getfunctions )( &p_module->p_functions->dec );
93 MODULE_DEACTIVATE_START
94 MODULE_DEACTIVATE_STOP
97 /*****************************************************************************
98 * decoder_Probe: probe the decoder and return score
99 *****************************************************************************
100 * Tries to launch a decoder and return score so that the interface is able
102 *****************************************************************************/
103 static int decoder_Probe( u8 *pi_type )
105 return ( *pi_type == AC3_AUDIO_ES ) ? 0 : -1;
109 /*****************************************************************************
110 * InitThread: initialize data before entering main loop
111 *****************************************************************************/
112 static int InitThread( ac3dec_thread_t * p_ac3thread )
119 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
120 p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
123 * Choose the best downmix module
125 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
126 psz_name = config_GetPszVariable( DOWNMIX_METHOD_VAR );
127 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, psz_name,
129 if( psz_name ) free( psz_name );
131 if( DOWNMIX.p_module == NULL )
133 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
134 free( p_ac3thread->ac3_decoder );
138 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
139 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
140 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
141 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
142 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
143 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
144 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
145 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
150 * Choose the best IMDCT module
152 p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
154 #define IMDCT p_ac3thread->ac3_decoder->imdct
155 psz_name = config_GetPszVariable( IMDCT_METHOD_VAR );
156 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, psz_name,
158 if( psz_name ) free( psz_name );
160 if( IMDCT->p_module == NULL )
162 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
163 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
164 free( p_ac3thread->ac3_decoder->imdct );
165 free( p_ac3thread->ac3_decoder );
169 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
170 IMDCT->pf_imdct_init = F.pf_imdct_init;
171 IMDCT->pf_imdct_256 = F.pf_imdct_256;
172 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
173 IMDCT->pf_imdct_512 = F.pf_imdct_512;
174 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
177 /* Initialize the ac3 decoder structures */
178 p_ac3thread->ac3_decoder->samples = memalign( 16, 6 * 256 * sizeof(float) );
180 IMDCT->buf = memalign( 16, N/4 * sizeof(complex_t) );
181 IMDCT->delay = memalign( 16, 6 * 256 * sizeof(float) );
182 IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
183 IMDCT->xcos1 = memalign( 16, N/4 * sizeof(float) );
184 IMDCT->xsin1 = memalign( 16, N/4 * sizeof(float) );
185 IMDCT->xcos2 = memalign( 16, N/8 * sizeof(float) );
186 IMDCT->xsin2 = memalign( 16, N/8 * sizeof(float) );
187 IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
188 IMDCT->w_1 = memalign( 16, 1 * sizeof(complex_t) );
189 IMDCT->w_2 = memalign( 16, 2 * sizeof(complex_t) );
190 IMDCT->w_4 = memalign( 16, 4 * sizeof(complex_t) );
191 IMDCT->w_8 = memalign( 16, 8 * sizeof(complex_t) );
192 IMDCT->w_16 = memalign( 16, 16 * sizeof(complex_t) );
193 IMDCT->w_32 = memalign( 16, 32 * sizeof(complex_t) );
194 IMDCT->w_64 = memalign( 16, 64 * sizeof(complex_t) );
196 ac3_init( p_ac3thread->ac3_decoder );
199 * Initialize the output properties
201 p_ac3thread->p_aout_fifo = NULL;
206 InitBitstream(&p_ac3thread->ac3_decoder->bit_stream,
207 p_ac3thread->p_config->p_decoder_fifo,
208 BitstreamCallback, (void *) 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;
219 boolean_t b_sync = 0;
221 /* Allocate the memory needed to store the thread's structure */
222 p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
224 if( p_ac3thread == NULL )
226 intf_ErrMsg ( "ac3_adec error: not enough memory "
227 "for decoder_Run() to allocate p_ac3thread" );
228 DecoderError( p_config->p_decoder_fifo );
233 * Initialize the thread properties
235 p_ac3thread->p_config = p_config;
236 if( InitThread( p_ac3thread ) )
238 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
239 DecoderError( p_config->p_decoder_fifo );
244 /* ac3 decoder thread's main loop */
245 /* FIXME : do we have enough room to store the decoded frames ?? */
246 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
249 ac3_sync_info_t sync_info;
254 #define p_bit_stream (&p_ac3thread->ac3_decoder->bit_stream)
256 /* Go to the next PES packet and jump to sync_ptr */
258 BitstreamNextDataPacket( p_bit_stream );
259 } while( !p_bit_stream->p_decoder_fifo->b_die
260 && !p_bit_stream->p_decoder_fifo->b_error
261 && p_bit_stream->p_data !=
262 p_bit_stream->p_decoder_fifo->p_first->p_first );
263 i_sync_ptr = *(p_bit_stream->p_byte - 2) << 8
264 | *(p_bit_stream->p_byte - 1);
265 p_bit_stream->p_byte += i_sync_ptr;
267 /* Empty the bit FIFO and realign the bit stream */
268 p_bit_stream->fifo.buffer = 0;
269 p_bit_stream->fifo.i_available = 0;
270 AlignWord( p_bit_stream );
275 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
281 if( ( p_ac3thread->p_aout_fifo != NULL ) &&
282 ( p_ac3thread->p_aout_fifo->i_rate != sync_info.sample_rate ) )
284 /* Make sure the output thread leaves the NextFrame() function */
285 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
286 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
287 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
288 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
290 p_ac3thread->p_aout_fifo = NULL;
293 /* Creating the audio output fifo if not created yet */
294 if (p_ac3thread->p_aout_fifo == NULL ) {
295 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_FIFO_PCM, 2,
296 sync_info.sample_rate, AC3DEC_FRAME_SIZE, NULL );
297 if ( p_ac3thread->p_aout_fifo == NULL )
306 free( IMDCT->xcos_sin_sse );
307 free( IMDCT->xsin2 );
308 free( IMDCT->xcos2 );
309 free( IMDCT->xsin1 );
310 free( IMDCT->xcos1 );
311 free( IMDCT->delay1 );
312 free( IMDCT->delay );
316 free( p_ac3thread->ac3_decoder->samples );
318 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
319 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
321 free( p_ac3thread->ac3_decoder->imdct );
322 free( p_ac3thread->ac3_decoder );
328 CurrentPTS( &p_ac3thread->ac3_decoder->bit_stream,
329 &p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame],
331 if( !p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame] )
333 p_ac3thread->p_aout_fifo->date[
334 p_ac3thread->p_aout_fifo->i_end_frame] =
338 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
339 (p_ac3thread->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
341 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
347 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
348 p_ac3thread->p_aout_fifo->i_end_frame =
349 (p_ac3thread->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
350 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
351 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
353 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
356 /* If b_error is set, the ac3 decoder thread enters the error loop */
357 if (p_ac3thread->p_fifo->b_error)
359 DecoderError( p_ac3thread->p_fifo );
362 /* End of the ac3 decoder thread */
363 EndThread (p_ac3thread);
371 /*****************************************************************************
372 * EndThread : ac3 decoder thread destruction
373 *****************************************************************************/
374 static void EndThread (ac3dec_thread_t * p_ac3thread)
376 /* If the audio output fifo was created, we destroy it */
377 if (p_ac3thread->p_aout_fifo != NULL)
379 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
381 /* Make sure the output thread leaves the NextFrame() function */
382 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
383 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
384 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
387 /* Free allocated structures */
388 #define IMDCT p_ac3thread->ac3_decoder->imdct
396 free( IMDCT->xcos_sin_sse );
397 free( IMDCT->xsin2 );
398 free( IMDCT->xcos2 );
399 free( IMDCT->xsin1 );
400 free( IMDCT->xcos1 );
401 free( IMDCT->delay1 );
402 free( IMDCT->delay );
406 free( p_ac3thread->ac3_decoder->samples );
408 /* Unlock the modules */
409 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
410 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
412 /* Free what's left of the decoder */
413 free( p_ac3thread->ac3_decoder->imdct );
414 free( p_ac3thread->ac3_decoder );
417 /*****************************************************************************
418 * BitstreamCallback: Import parameters from the new data/PES packet
419 *****************************************************************************
420 * This function is called by input's NextDataPacket.
421 *****************************************************************************/
422 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
423 boolean_t b_new_pes )
427 /* Drop special AC3 header */
428 p_bit_stream->p_byte += 3;