]> git.sesse.net Git - vlc/blob - plugins/ac3_adec/ac3_adec.c
* Fixed a quite old bug in the audio output which made the sound stutter
[vlc] / plugins / ac3_adec / ac3_adec.c
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 $
6  *
7  * Authors: Michel Lespinasse <walken@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                              /* memset() */
29
30 #include <videolan/vlc.h>
31
32 #ifdef HAVE_UNISTD_H
33 #   include <unistd.h>                                           /* getpid() */
34 #endif
35
36 #include "audio_output.h"
37
38 #include "stream_control.h"
39 #include "input_ext-dec.h"
40 #include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
41
42 #include "ac3_imdct.h"
43 #include "ac3_downmix.h"
44 #include "ac3_decoder.h"
45 #include "ac3_adec.h"
46
47 #define AC3DEC_FRAME_SIZE (2*1536) 
48
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 );
58
59 /*****************************************************************************
60  * Capabilities
61  *****************************************************************************/
62 void _M( adec_getfunctions )( function_list_t * p_function_list )
63 {
64     p_function_list->functions.dec.pf_probe = decoder_Probe;
65     p_function_list->functions.dec.pf_run   = decoder_Run;
66 }
67
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"
75
76 MODULE_CONFIG_START
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 )
82 MODULE_CONFIG_STOP
83
84 MODULE_INIT_START
85     SET_DESCRIPTION( "software AC3 decoder" )
86     ADD_CAPABILITY( DECODER, 50 )
87 MODULE_INIT_STOP
88
89 MODULE_ACTIVATE_START
90     _M( adec_getfunctions )( &p_module->p_functions->dec );
91 MODULE_ACTIVATE_STOP
92
93 MODULE_DEACTIVATE_START
94 MODULE_DEACTIVATE_STOP
95
96
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 
101  * to chose.
102  *****************************************************************************/
103 static int decoder_Probe( u8 *pi_type )
104 {
105     return ( *pi_type == AC3_AUDIO_ES ) ? 0 : -1;
106 }
107
108
109 /*****************************************************************************
110  * InitThread: initialize data before entering main loop
111  *****************************************************************************/
112 static int InitThread( ac3dec_thread_t * p_ac3thread )
113 {
114     char *psz_name;
115
116     /*
117      * Thread properties 
118      */
119     p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
120     p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
121
122     /*
123      * Choose the best downmix module
124      */
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,
128                                     NULL );
129     if( psz_name ) free( psz_name );
130
131     if( DOWNMIX.p_module == NULL )
132     {
133         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
134         free( p_ac3thread->ac3_decoder );
135         return( -1 );
136     }
137
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;
146 #undef F
147 #undef DOWNMIX
148
149     /*
150      * Choose the best IMDCT module
151      */
152     p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
153     
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,
157                                    NULL );
158     if( psz_name ) free( psz_name );
159
160     if( IMDCT->p_module == NULL )
161     {
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 );
166         return( -1 );
167     }
168
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;
175 #undef F
176
177     /* Initialize the ac3 decoder structures */
178     p_ac3thread->ac3_decoder->samples = memalign( 16, 6 * 256 * sizeof(float) );
179
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) );
195
196     ac3_init( p_ac3thread->ac3_decoder );
197
198     /*
199      * Initialize the output properties
200      */
201     p_ac3thread->p_aout_fifo = NULL;
202
203     /*
204      * Bit stream
205      */
206     InitBitstream(&p_ac3thread->ac3_decoder->bit_stream,
207             p_ac3thread->p_config->p_decoder_fifo,
208             BitstreamCallback, (void *) p_ac3thread );
209     
210     return( 0 );
211 }
212
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 )
217 {
218     ac3dec_thread_t *   p_ac3thread;
219     boolean_t           b_sync = 0;
220
221     /* Allocate the memory needed to store the thread's structure */
222     p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
223
224     if( p_ac3thread == NULL )
225     {
226         intf_ErrMsg ( "ac3_adec error: not enough memory "
227                       "for decoder_Run() to allocate p_ac3thread" );
228         DecoderError( p_config->p_decoder_fifo );
229         return( -1 );
230     }
231
232     /*
233      * Initialize the thread properties
234      */
235     p_ac3thread->p_config = p_config;
236     if( InitThread( p_ac3thread ) )
237     {
238         intf_ErrMsg( "ac3_adec error: could not initialize thread" );
239         DecoderError( p_config->p_decoder_fifo );
240         free( p_ac3thread );
241         return( -1 );
242     }
243
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))
247     {
248         s16 * buffer;
249         ac3_sync_info_t sync_info;
250
251         if( !b_sync )
252         {
253              int i_sync_ptr;
254 #define p_bit_stream (&p_ac3thread->ac3_decoder->bit_stream)
255
256              /* Go to the next PES packet and jump to sync_ptr */
257              do {
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;
266
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 );
271              b_sync = 1;
272 #undef p_bit_stream
273         }
274
275         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
276         {
277             b_sync = 0;
278             continue;
279         }
280
281         if( ( p_ac3thread->p_aout_fifo != NULL ) &&
282             ( p_ac3thread->p_aout_fifo->i_rate != sync_info.sample_rate ) )
283         {
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));
289
290             p_ac3thread->p_aout_fifo = NULL;
291         }
292
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 )
298             {
299                 free( IMDCT->w_1 );
300                 free( IMDCT->w_64 );
301                 free( IMDCT->w_32 );
302                 free( IMDCT->w_16 );
303                 free( IMDCT->w_8 );
304                 free( IMDCT->w_4 );
305                 free( IMDCT->w_2 );
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 );
313                 free( IMDCT->buf );
314         #undef IMDCT
315
316                 free( p_ac3thread->ac3_decoder->samples );
317         
318                 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
319                 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
320         
321                 free( p_ac3thread->ac3_decoder->imdct );
322                 free( p_ac3thread->ac3_decoder );
323         
324                 return( -1 );
325             }
326         }
327
328         CurrentPTS( &p_ac3thread->ac3_decoder->bit_stream,
329             &p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame],
330             NULL );
331         if( !p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame] )
332         {
333             p_ac3thread->p_aout_fifo->date[
334                 p_ac3thread->p_aout_fifo->i_end_frame] =
335                 LAST_MDATE;
336         }
337     
338         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
339             (p_ac3thread->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
340
341         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
342         {
343             b_sync = 0;
344             continue;
345         }
346         
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);
352
353         RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
354     }
355
356     /* If b_error is set, the ac3 decoder thread enters the error loop */
357     if (p_ac3thread->p_fifo->b_error)
358     {
359         DecoderError( p_ac3thread->p_fifo );
360     }
361
362     /* End of the ac3 decoder thread */
363     EndThread (p_ac3thread);
364
365     free( p_ac3thread );
366
367     return( 0 );
368 }
369
370
371 /*****************************************************************************
372  * EndThread : ac3 decoder thread destruction
373  *****************************************************************************/
374 static void EndThread (ac3dec_thread_t * p_ac3thread)
375 {
376     /* If the audio output fifo was created, we destroy it */
377     if (p_ac3thread->p_aout_fifo != NULL)
378     {
379         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
380
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));
385     }
386
387     /* Free allocated structures */
388 #define IMDCT p_ac3thread->ac3_decoder->imdct
389     free( IMDCT->w_1 );
390     free( IMDCT->w_64 );
391     free( IMDCT->w_32 );
392     free( IMDCT->w_16 );
393     free( IMDCT->w_8 );
394     free( IMDCT->w_4 );
395     free( IMDCT->w_2 );
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 );
403     free( IMDCT->buf );
404 #undef IMDCT
405
406     free( p_ac3thread->ac3_decoder->samples );
407
408     /* Unlock the modules */
409     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
410     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
411
412     /* Free what's left of the decoder */
413     free( p_ac3thread->ac3_decoder->imdct );
414     free( p_ac3thread->ac3_decoder );
415 }
416
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 )
424 {
425     if( b_new_pes )
426     {
427         /* Drop special AC3 header */
428         p_bit_stream->p_byte += 3;
429     }
430 }
431