* ./src/misc/modules_plugin.h: exported input_ClockManageRef for fenrir.
[vlc.git] / plugins / ac3_adec / ac3_adec.c
blob9aae57fad8a399282e5c876f7c35f6533a17ec7e
1 /*****************************************************************************
2 * ac3_adec.c: ac3 decoder module main file
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: ac3_adec.c,v 1.29 2002/04/25 21:52:42 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 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memset() */
30 #include <videolan/vlc.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h> /* getpid() */
34 #endif
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"
45 #include "ac3_adec.h"
47 #define AC3DEC_FRAME_SIZE (2*1536)
49 /*****************************************************************************
50 * Local prototypes
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 /*****************************************************************************
60 * Capabilities
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"
76 MODULE_CONFIG_START
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 )
82 MODULE_CONFIG_STOP
84 MODULE_INIT_START
85 SET_DESCRIPTION( _("software AC3 decoder") )
86 ADD_CAPABILITY( DECODER, 50 )
87 ADD_SHORTCUT( "ac3_adec" )
88 ADD_SHORTCUT( "ac3" )
89 MODULE_INIT_STOP
91 MODULE_ACTIVATE_START
92 _M( adec_getfunctions )( &p_module->p_functions->dec );
93 MODULE_ACTIVATE_STOP
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
103 * to chose.
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 )
116 char *psz_name;
119 * Thread properties
121 p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
122 p_ac3thread->ac3_decoder =
123 vlc_memalign( &p_ac3thread->ac3_decoder_orig, 16, sizeof(ac3dec_t) );
126 * Choose the best downmix module
128 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
129 psz_name = config_GetPszVariable( DOWNMIX_METHOD_VAR );
130 DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, psz_name,
131 NULL );
132 if( psz_name ) free( psz_name );
134 if( DOWNMIX.p_module == NULL )
136 intf_ErrMsg( "ac3dec error: no suitable downmix module" );
137 free( p_ac3thread->ac3_decoder_orig );
138 return( -1 );
141 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
142 DOWNMIX.pf_downmix_3f_2r_to_2ch = F.pf_downmix_3f_2r_to_2ch;
143 DOWNMIX.pf_downmix_2f_2r_to_2ch = F.pf_downmix_2f_2r_to_2ch;
144 DOWNMIX.pf_downmix_3f_1r_to_2ch = F.pf_downmix_3f_1r_to_2ch;
145 DOWNMIX.pf_downmix_2f_1r_to_2ch = F.pf_downmix_2f_1r_to_2ch;
146 DOWNMIX.pf_downmix_3f_0r_to_2ch = F.pf_downmix_3f_0r_to_2ch;
147 DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
148 DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
149 #undef F
150 #undef DOWNMIX
153 * Choose the best IMDCT module
155 p_ac3thread->ac3_decoder->imdct =
156 vlc_memalign( &p_ac3thread->ac3_decoder->imdct_orig, 16, sizeof(imdct_t) );
158 #define IMDCT p_ac3thread->ac3_decoder->imdct
159 psz_name = config_GetPszVariable( IMDCT_METHOD_VAR );
160 IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, psz_name,
161 NULL );
162 if( psz_name ) free( psz_name );
164 if( IMDCT->p_module == NULL )
166 intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
167 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
168 free( p_ac3thread->ac3_decoder->imdct_orig );
169 free( p_ac3thread->ac3_decoder_orig );
170 return( -1 );
173 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
174 IMDCT->pf_imdct_init = F.pf_imdct_init;
175 IMDCT->pf_imdct_256 = F.pf_imdct_256;
176 IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
177 IMDCT->pf_imdct_512 = F.pf_imdct_512;
178 IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
179 #undef F
181 /* Initialize the ac3 decoder structures */
182 p_ac3thread->ac3_decoder->samples =
183 vlc_memalign( &p_ac3thread->ac3_decoder->samples_orig,
184 16, 6 * 256 * sizeof(float) );
186 IMDCT->buf = vlc_memalign( &IMDCT->buf_orig,
187 16, N/4 * sizeof(complex_t) );
188 IMDCT->delay = vlc_memalign( &IMDCT->delay_orig,
189 16, 6 * 256 * sizeof(float) );
190 IMDCT->delay1 = vlc_memalign( &IMDCT->delay1_orig,
191 16, 6 * 256 * sizeof(float) );
192 IMDCT->xcos1 = vlc_memalign( &IMDCT->xcos1_orig,
193 16, N/4 * sizeof(float) );
194 IMDCT->xsin1 = vlc_memalign( &IMDCT->xsin1_orig,
195 16, N/4 * sizeof(float) );
196 IMDCT->xcos2 = vlc_memalign( &IMDCT->xcos2_orig,
197 16, N/8 * sizeof(float) );
198 IMDCT->xsin2 = vlc_memalign( &IMDCT->xsin2_orig,
199 16, N/8 * sizeof(float) );
200 IMDCT->xcos_sin_sse = vlc_memalign( &IMDCT->xcos_sin_sse_orig,
201 16, 128 * 4 * sizeof(float) );
202 IMDCT->w_1 = vlc_memalign( &IMDCT->w_1_orig,
203 16, 1 * sizeof(complex_t) );
204 IMDCT->w_2 = vlc_memalign( &IMDCT->w_2_orig,
205 16, 2 * sizeof(complex_t) );
206 IMDCT->w_4 = vlc_memalign( &IMDCT->w_4_orig,
207 16, 4 * sizeof(complex_t) );
208 IMDCT->w_8 = vlc_memalign( &IMDCT->w_8_orig,
209 16, 8 * sizeof(complex_t) );
210 IMDCT->w_16 = vlc_memalign( &IMDCT->w_16_orig,
211 16, 16 * sizeof(complex_t) );
212 IMDCT->w_32 = vlc_memalign( &IMDCT->w_32_orig,
213 16, 32 * sizeof(complex_t) );
214 IMDCT->w_64 = vlc_memalign( &IMDCT->w_64_orig,
215 16, 64 * sizeof(complex_t) );
217 _M( ac3_init )( p_ac3thread->ac3_decoder );
220 * Initialize the output properties
222 p_ac3thread->p_aout_fifo = NULL;
225 * Bit stream
227 InitBitstream(&p_ac3thread->ac3_decoder->bit_stream,
228 p_ac3thread->p_config->p_decoder_fifo,
229 BitstreamCallback, (void *) p_ac3thread );
231 return( 0 );
234 /*****************************************************************************
235 * decoder_Run: this function is called just after the thread is created
236 *****************************************************************************/
237 static int decoder_Run ( decoder_config_t * p_config )
239 ac3dec_thread_t * p_ac3thread;
240 void * p_ac3thread_orig; /* pointer before memalign */
241 boolean_t b_sync = 0;
243 /* Allocate the memory needed to store the thread's structure */
244 p_ac3thread = (ac3dec_thread_t *)vlc_memalign( &p_ac3thread_orig, 16,
245 sizeof(ac3dec_thread_t) );
247 if( p_ac3thread == NULL )
249 intf_ErrMsg ( "ac3_adec error: not enough memory "
250 "for decoder_Run() to allocate p_ac3thread" );
251 DecoderError( p_config->p_decoder_fifo );
252 return( -1 );
256 * Initialize the thread properties
258 p_ac3thread->p_config = p_config;
259 if( InitThread( p_ac3thread ) )
261 intf_ErrMsg( "ac3_adec error: could not initialize thread" );
262 DecoderError( p_config->p_decoder_fifo );
263 free( p_ac3thread_orig );
264 return( -1 );
267 /* ac3 decoder thread's main loop */
268 /* FIXME : do we have enough room to store the decoded frames ?? */
269 while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
271 s16 * buffer;
272 ac3_sync_info_t sync_info;
274 if( !b_sync )
276 int i_sync_ptr;
277 #define p_bit_stream (&p_ac3thread->ac3_decoder->bit_stream)
279 /* Go to the next PES packet and jump to sync_ptr */
280 do {
281 BitstreamNextDataPacket( p_bit_stream );
282 } while( !p_bit_stream->p_decoder_fifo->b_die
283 && !p_bit_stream->p_decoder_fifo->b_error
284 && p_bit_stream->p_data !=
285 p_bit_stream->p_decoder_fifo->p_first->p_first );
286 i_sync_ptr = *(p_bit_stream->p_byte - 2) << 8
287 | *(p_bit_stream->p_byte - 1);
288 p_bit_stream->p_byte += i_sync_ptr;
290 /* Empty the bit FIFO and realign the bit stream */
291 p_bit_stream->fifo.buffer = 0;
292 p_bit_stream->fifo.i_available = 0;
293 AlignWord( p_bit_stream );
294 b_sync = 1;
295 #undef p_bit_stream
298 if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
300 b_sync = 0;
301 continue;
304 if( ( p_ac3thread->p_aout_fifo != NULL ) &&
305 ( p_ac3thread->p_aout_fifo->i_rate != sync_info.sample_rate ) )
307 /* Make sure the output thread leaves the NextFrame() function */
308 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
309 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
310 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
311 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
313 p_ac3thread->p_aout_fifo = NULL;
316 /* Creating the audio output fifo if not created yet */
317 if (p_ac3thread->p_aout_fifo == NULL ) {
318 p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_FIFO_PCM, 2,
319 sync_info.sample_rate, AC3DEC_FRAME_SIZE, NULL );
320 if ( p_ac3thread->p_aout_fifo == NULL )
322 free( IMDCT->w_1_orig );
323 free( IMDCT->w_64_orig );
324 free( IMDCT->w_32_orig );
325 free( IMDCT->w_16_orig );
326 free( IMDCT->w_8_orig );
327 free( IMDCT->w_4_orig );
328 free( IMDCT->w_2_orig );
329 free( IMDCT->xcos_sin_sse_orig );
330 free( IMDCT->xsin2_orig );
331 free( IMDCT->xcos2_orig );
332 free( IMDCT->xsin1_orig );
333 free( IMDCT->xcos1_orig );
334 free( IMDCT->delay1_orig );
335 free( IMDCT->delay_orig );
336 free( IMDCT->buf_orig );
337 #undef IMDCT
339 free( p_ac3thread->ac3_decoder->samples_orig );
341 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
342 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
344 free( p_ac3thread->ac3_decoder->imdct_orig );
345 free( p_ac3thread->ac3_decoder_orig );
347 return( -1 );
351 CurrentPTS( &p_ac3thread->ac3_decoder->bit_stream,
352 &p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame],
353 NULL );
354 if( !p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame] )
356 p_ac3thread->p_aout_fifo->date[
357 p_ac3thread->p_aout_fifo->i_end_frame] =
358 LAST_MDATE;
361 buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) +
362 (p_ac3thread->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
364 if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
366 b_sync = 0;
367 continue;
370 vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
371 p_ac3thread->p_aout_fifo->i_end_frame =
372 (p_ac3thread->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
373 vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
374 vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
376 RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
379 /* If b_error is set, the ac3 decoder thread enters the error loop */
380 if (p_ac3thread->p_fifo->b_error)
382 DecoderError( p_ac3thread->p_fifo );
385 /* End of the ac3 decoder thread */
386 EndThread (p_ac3thread);
388 free( p_ac3thread_orig );
390 return( 0 );
394 /*****************************************************************************
395 * EndThread : ac3 decoder thread destruction
396 *****************************************************************************/
397 static void EndThread (ac3dec_thread_t * p_ac3thread)
399 /* If the audio output fifo was created, we destroy it */
400 if (p_ac3thread->p_aout_fifo != NULL)
402 aout_DestroyFifo (p_ac3thread->p_aout_fifo);
404 /* Make sure the output thread leaves the NextFrame() function */
405 vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
406 vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
407 vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
410 /* Free allocated structures */
411 #define IMDCT p_ac3thread->ac3_decoder->imdct
412 free( IMDCT->w_1_orig );
413 free( IMDCT->w_64_orig );
414 free( IMDCT->w_32_orig );
415 free( IMDCT->w_16_orig );
416 free( IMDCT->w_8_orig );
417 free( IMDCT->w_4_orig );
418 free( IMDCT->w_2_orig );
419 free( IMDCT->xcos_sin_sse_orig );
420 free( IMDCT->xsin2_orig );
421 free( IMDCT->xcos2_orig );
422 free( IMDCT->xsin1_orig );
423 free( IMDCT->xcos1_orig );
424 free( IMDCT->delay1_orig );
425 free( IMDCT->delay_orig );
426 free( IMDCT->buf_orig );
427 #undef IMDCT
429 free( p_ac3thread->ac3_decoder->samples_orig );
431 /* Unlock the modules */
432 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
433 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
435 /* Free what's left of the decoder */
436 free( p_ac3thread->ac3_decoder->imdct_orig );
437 free( p_ac3thread->ac3_decoder_orig );
440 /*****************************************************************************
441 * BitstreamCallback: Import parameters from the new data/PES packet
442 *****************************************************************************
443 * This function is called by input's NextDataPacket.
444 *****************************************************************************/
445 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
446 boolean_t b_new_pes )
448 if( b_new_pes )
450 /* Drop special AC3 header */
451 p_bit_stream->p_byte += 3;