1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_adec.c,v 1.30 2002/05/15 19:36:04 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( N_("Miscellaneous"), NULL)
78 ADD_MODULE ( DOWNMIX_METHOD_VAR, MODULE_CAPABILITY_DOWNMIX, NULL, NULL,
79 N_("AC3 downmix module"), NULL )
80 ADD_MODULE ( IMDCT_METHOD_VAR, MODULE_CAPABILITY_IMDCT, NULL, NULL,
81 N_("AC3 IMDCT module"), NULL )
85 SET_DESCRIPTION( _("software AC3 decoder") )
86 ADD_CAPABILITY( DECODER, 50 )
87 ADD_SHORTCUT( "ac3_adec" )
92 _M( adec_getfunctions )( &p_module->p_functions->dec );
95 MODULE_DEACTIVATE_START
96 MODULE_DEACTIVATE_STOP
99 /*****************************************************************************
100 * decoder_Probe: probe the decoder and return score
101 *****************************************************************************
102 * Tries to launch a decoder and return score so that the interface is able
104 *****************************************************************************/
105 static int decoder_Probe( u8 *pi_type )
107 return ( *pi_type == AC3_AUDIO_ES ) ? 0 : -1;
111 /*****************************************************************************
112 * InitThread: initialize data before entering main loop
113 *****************************************************************************/
114 static int InitThread( ac3dec_thread_t * p_ac3thread )
121 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
123 p_ac3thread->ac3_decoder =
124 vlc_memalign( &p_ac3thread->ac3_decoder_orig, 16, sizeof(ac3dec_t) );
125 memset( p_ac3thread->ac3_decoder, 0, sizeof( ac3dec_t ) );
128 * Choose the best downmix module
130 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
131 psz_name = config_GetPszVariable( DOWNMIX_METHOD_VAR );
132 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, psz_name,
134 if( psz_name ) free( psz_name );
136 if( DOWNMIX.p_module == NULL )
138 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
139 free( p_ac3thread->ac3_decoder_orig );
143 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
144 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
145 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
146 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
147 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
148 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
149 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
150 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
155 * Choose the best IMDCT module
157 p_ac3thread->ac3_decoder->imdct =
158 vlc_memalign( &p_ac3thread->ac3_decoder->imdct_orig, 16, sizeof(imdct_t) );
160 #define IMDCT p_ac3thread->ac3_decoder->imdct
161 psz_name = config_GetPszVariable( IMDCT_METHOD_VAR );
162 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, psz_name,
164 if( psz_name ) free( psz_name );
166 if( IMDCT->p_module == NULL )
168 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
169 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
170 free( p_ac3thread->ac3_decoder->imdct_orig );
171 free( p_ac3thread->ac3_decoder_orig );
175 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
176 IMDCT->pf_imdct_init = F.pf_imdct_init;
177 IMDCT->pf_imdct_256 = F.pf_imdct_256;
178 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
179 IMDCT->pf_imdct_512 = F.pf_imdct_512;
180 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
183 /* Initialize the ac3 decoder structures */
184 p_ac3thread->ac3_decoder->samples =
185 vlc_memalign( &p_ac3thread->ac3_decoder->samples_orig,
186 16, 6 * 256 * sizeof(float) );
188 IMDCT->buf = vlc_memalign( &IMDCT->buf_orig,
189 16, N/4 * sizeof(complex_t) );
190 IMDCT->delay = vlc_memalign( &IMDCT->delay_orig,
191 16, 6 * 256 * sizeof(float) );
192 IMDCT->delay1 = vlc_memalign( &IMDCT->delay1_orig,
193 16, 6 * 256 * sizeof(float) );
194 IMDCT->xcos1 = vlc_memalign( &IMDCT->xcos1_orig,
195 16, N/4 * sizeof(float) );
196 IMDCT->xsin1 = vlc_memalign( &IMDCT->xsin1_orig,
197 16, N/4 * sizeof(float) );
198 IMDCT->xcos2 = vlc_memalign( &IMDCT->xcos2_orig,
199 16, N/8 * sizeof(float) );
200 IMDCT->xsin2 = vlc_memalign( &IMDCT->xsin2_orig,
201 16, N/8 * sizeof(float) );
202 IMDCT->xcos_sin_sse = vlc_memalign( &IMDCT->xcos_sin_sse_orig,
203 16, 128 * 4 * sizeof(float) );
204 IMDCT->w_1 = vlc_memalign( &IMDCT->w_1_orig,
205 16, 1 * sizeof(complex_t) );
206 IMDCT->w_2 = vlc_memalign( &IMDCT->w_2_orig,
207 16, 2 * sizeof(complex_t) );
208 IMDCT->w_4 = vlc_memalign( &IMDCT->w_4_orig,
209 16, 4 * sizeof(complex_t) );
210 IMDCT->w_8 = vlc_memalign( &IMDCT->w_8_orig,
211 16, 8 * sizeof(complex_t) );
212 IMDCT->w_16 = vlc_memalign( &IMDCT->w_16_orig,
213 16, 16 * sizeof(complex_t) );
214 IMDCT->w_32 = vlc_memalign( &IMDCT->w_32_orig,
215 16, 32 * sizeof(complex_t) );
216 IMDCT->w_64 = vlc_memalign( &IMDCT->w_64_orig,
217 16, 64 * sizeof(complex_t) );
220 _M( ac3_init )( p_ac3thread->ac3_decoder );
223 * Initialize the output properties
225 p_ac3thread->p_aout_fifo = NULL;
230 InitBitstream(&p_ac3thread->ac3_decoder->bit_stream,
231 p_ac3thread->p_config->p_decoder_fifo,
232 BitstreamCallback, (void *) p_ac3thread );
237 /*****************************************************************************
238 * decoder_Run: this function is called just after the thread is created
239 *****************************************************************************/
240 static int decoder_Run ( decoder_config_t * p_config )
242 ac3dec_thread_t * p_ac3thread;
243 void * p_ac3thread_orig; /* pointer before memalign */
244 boolean_t b_sync = 0;
246 /* Allocate the memory needed to store the thread's structure */
247 p_ac3thread = (ac3dec_thread_t *)vlc_memalign( &p_ac3thread_orig, 16,
248 sizeof(ac3dec_thread_t) );
250 if( p_ac3thread == NULL )
252 intf_ErrMsg ( "ac3_adec error: not enough memory "
253 "for decoder_Run() to allocate p_ac3thread" );
254 DecoderError( p_config->p_decoder_fifo );
259 * Initialize the thread properties
261 p_ac3thread->p_config = p_config;
262 if( InitThread( p_ac3thread ) )
264 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
265 DecoderError( p_config->p_decoder_fifo );
266 free( p_ac3thread_orig );
270 /* ac3 decoder thread's main loop */
271 /* FIXME : do we have enough room to store the decoded frames ?? */
272 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
275 ac3_sync_info_t sync_info;
280 #define p_bit_stream (&p_ac3thread->ac3_decoder->bit_stream)
282 /* Go to the next PES packet and jump to sync_ptr */
284 BitstreamNextDataPacket( p_bit_stream );
285 } while( !p_bit_stream->p_decoder_fifo->b_die
286 && !p_bit_stream->p_decoder_fifo->b_error
287 && p_bit_stream->p_data !=
288 p_bit_stream->p_decoder_fifo->p_first->p_first );
289 i_sync_ptr = *(p_bit_stream->p_byte - 2) << 8
290 | *(p_bit_stream->p_byte - 1);
291 p_bit_stream->p_byte += i_sync_ptr;
293 /* Empty the bit FIFO and realign the bit stream */
294 p_bit_stream->fifo.buffer = 0;
295 p_bit_stream->fifo.i_available = 0;
296 AlignWord( p_bit_stream );
301 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
307 if( ( p_ac3thread->p_aout_fifo != NULL ) &&
308 ( p_ac3thread->p_aout_fifo->i_rate != sync_info.sample_rate ) )
310 /* Make sure the output thread leaves the NextFrame() function */
311 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
312 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
313 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
314 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
316 p_ac3thread->p_aout_fifo = NULL;
319 /* Creating the audio output fifo if not created yet */
320 if (p_ac3thread->p_aout_fifo == NULL )
322 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_FIFO_PCM, 2,
323 sync_info.sample_rate, AC3DEC_FRAME_SIZE, NULL );
324 if ( p_ac3thread->p_aout_fifo == NULL )
326 p_ac3thread->p_fifo->b_error = 1;
331 CurrentPTS( &p_ac3thread->ac3_decoder->bit_stream,
332 &p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame],
334 if( !p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame] )
336 p_ac3thread->p_aout_fifo->date[
337 p_ac3thread->p_aout_fifo->i_end_frame] =
341 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
342 (p_ac3thread->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
344 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
350 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
351 p_ac3thread->p_aout_fifo->i_end_frame =
352 (p_ac3thread->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
353 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
354 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
356 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
359 /* If b_error is set, the ac3 decoder thread enters the error loop */
360 if (p_ac3thread->p_fifo->b_error)
362 DecoderError( p_ac3thread->p_fifo );
365 /* End of the ac3 decoder thread */
366 EndThread (p_ac3thread);
368 free( p_ac3thread_orig );
374 /*****************************************************************************
375 * EndThread : ac3 decoder thread destruction
376 *****************************************************************************/
377 static void EndThread (ac3dec_thread_t * p_ac3thread)
379 /* If the audio output fifo was created, we destroy it */
380 if (p_ac3thread->p_aout_fifo != NULL)
382 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
384 /* Make sure the output thread leaves the NextFrame() function */
385 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
386 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
387 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
390 /* Free allocated structures */
391 #define IMDCT p_ac3thread->ac3_decoder->imdct
392 free( IMDCT->w_1_orig );
393 free( IMDCT->w_64_orig );
394 free( IMDCT->w_32_orig );
395 free( IMDCT->w_16_orig );
396 free( IMDCT->w_8_orig );
397 free( IMDCT->w_4_orig );
398 free( IMDCT->w_2_orig );
399 free( IMDCT->xcos_sin_sse_orig );
400 free( IMDCT->xsin2_orig );
401 free( IMDCT->xcos2_orig );
402 free( IMDCT->xsin1_orig );
403 free( IMDCT->xcos1_orig );
404 free( IMDCT->delay1_orig );
405 free( IMDCT->delay_orig );
406 free( IMDCT->buf_orig );
409 free( p_ac3thread->ac3_decoder->samples_orig );
411 /* Unlock the modules */
412 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
413 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
415 /* Free what's left of the decoder */
416 free( p_ac3thread->ac3_decoder->imdct_orig );
417 free( p_ac3thread->ac3_decoder_orig );
420 /*****************************************************************************
421 * BitstreamCallback: Import parameters from the new data/PES packet
422 *****************************************************************************
423 * This function is called by input's NextDataPacket.
424 *****************************************************************************/
425 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
426 boolean_t b_new_pes )
430 /* Drop special AC3 header */
431 p_bit_stream->p_byte += 3;