]> git.sesse.net Git - vlc/blob - plugins/ac3_adec/ac3_adec.c
38a0dee63dd983e22dbd50d0d82609ea596776b8
[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.26 2002/04/21 10:32:20 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( N_("Miscellaneous"), NULL)
78 ADD_PLUGIN  ( DOWNMIX_METHOD_VAR, MODULE_CAPABILITY_DOWNMIX, NULL, NULL,
79               N_("AC3 downmix module"), NULL )
80 ADD_PLUGIN  ( IMDCT_METHOD_VAR, MODULE_CAPABILITY_IMDCT, NULL, NULL,
81               N_("AC3 IMDCT module"), NULL )
82 MODULE_CONFIG_STOP
83
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
90
91 MODULE_ACTIVATE_START
92     _M( adec_getfunctions )( &p_module->p_functions->dec );
93 MODULE_ACTIVATE_STOP
94
95 MODULE_DEACTIVATE_START
96 MODULE_DEACTIVATE_STOP
97
98
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 )
106 {
107     return ( *pi_type == AC3_AUDIO_ES ) ? 0 : -1;
108 }
109
110
111 /*****************************************************************************
112  * InitThread: initialize data before entering main loop
113  *****************************************************************************/
114 static int InitThread( ac3dec_thread_t * p_ac3thread )
115 {
116     char *psz_name;
117
118     /*
119      * Thread properties 
120      */
121     p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
122     p_ac3thread->ac3_decoder =
123         vlc_memalign( 16, sizeof(ac3dec_t), &p_ac3thread->ac3_decoder_orig );
124
125     /*
126      * Choose the best downmix module
127      */
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 );
133
134     if( DOWNMIX.p_module == NULL )
135     {
136         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
137         free( p_ac3thread->ac3_decoder_orig );
138         return( -1 );
139     }
140
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
151
152     /*
153      * Choose the best IMDCT module
154      */
155     p_ac3thread->ac3_decoder->imdct =
156     vlc_memalign( 16, sizeof(imdct_t), &p_ac3thread->ac3_decoder->imdct_orig );
157     
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 );
163
164     if( IMDCT->p_module == NULL )
165     {
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 );
171     }
172
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
180
181     /* Initialize the ac3 decoder structures */
182     p_ac3thread->ac3_decoder->samples =
183         vlc_memalign( 16, 6 * 256 * sizeof(float),
184                       &p_ac3thread->ac3_decoder->samples_orig );
185
186     IMDCT->buf    = vlc_memalign( 16, N/4 * sizeof(complex_t),
187                                   &IMDCT->buf_orig );
188     IMDCT->delay  = vlc_memalign( 16, 6 * 256 * sizeof(float),
189                                   &IMDCT->delay_orig );
190     IMDCT->delay1 = vlc_memalign( 16, 6 * 256 * sizeof(float),
191                                   &IMDCT->delay1_orig );
192     IMDCT->xcos1  = vlc_memalign( 16, N/4 * sizeof(float),
193                                   &IMDCT->xcos1_orig );
194     IMDCT->xsin1  = vlc_memalign( 16, N/4 * sizeof(float),
195                                   &IMDCT->xsin1_orig );
196     IMDCT->xcos2  = vlc_memalign( 16, N/8 * sizeof(float),
197                                   &IMDCT->xcos2_orig );
198     IMDCT->xsin2  = vlc_memalign( 16, N/8 * sizeof(float),
199                                   &IMDCT->xsin2_orig );
200     IMDCT->xcos_sin_sse = vlc_memalign( 16, 128 * 4 * sizeof(float),
201                                         &IMDCT->xcos_sin_sse_orig );
202     IMDCT->w_1    = vlc_memalign( 16, 1  * sizeof(complex_t),
203                                   &IMDCT->w_1_orig );
204     IMDCT->w_2    = vlc_memalign( 16, 2  * sizeof(complex_t),
205                                   &IMDCT->w_2_orig );
206     IMDCT->w_4    = vlc_memalign( 16, 4  * sizeof(complex_t),
207                                   &IMDCT->w_4_orig );
208     IMDCT->w_8    = vlc_memalign( 16, 8  * sizeof(complex_t),
209                                   &IMDCT->w_8_orig );
210     IMDCT->w_16   = vlc_memalign( 16, 16 * sizeof(complex_t),
211                                   &IMDCT->w_16_orig );
212     IMDCT->w_32   = vlc_memalign( 16, 32 * sizeof(complex_t),
213                                   &IMDCT->w_32_orig );
214     IMDCT->w_64   = vlc_memalign( 16, 64 * sizeof(complex_t),
215                                   &IMDCT->w_64_orig );
216
217     ac3_init( p_ac3thread->ac3_decoder );
218
219     /*
220      * Initialize the output properties
221      */
222     p_ac3thread->p_aout_fifo = NULL;
223
224     /*
225      * Bit stream
226      */
227     InitBitstream(&p_ac3thread->ac3_decoder->bit_stream,
228             p_ac3thread->p_config->p_decoder_fifo,
229             BitstreamCallback, (void *) p_ac3thread );
230     
231     return( 0 );
232 }
233
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 )
238 {
239     ac3dec_thread_t *   p_ac3thread;
240     void *              p_ac3thread_orig;         /* pointer before memalign */
241     boolean_t           b_sync = 0;
242
243     /* Allocate the memory needed to store the thread's structure */
244     p_ac3thread = (ac3dec_thread_t *)vlc_memalign( 16,
245                       sizeof(ac3dec_thread_t), &p_ac3thread_orig );
246
247     if( p_ac3thread == NULL )
248     {
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 );
253     }
254
255     /*
256      * Initialize the thread properties
257      */
258     p_ac3thread->p_config = p_config;
259     if( InitThread( p_ac3thread ) )
260     {
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 );
265     }
266
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))
270     {
271         s16 * buffer;
272         ac3_sync_info_t sync_info;
273
274         if( !b_sync )
275         {
276              int i_sync_ptr;
277 #define p_bit_stream (&p_ac3thread->ac3_decoder->bit_stream)
278
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;
289
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
296         }
297
298         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
299         {
300             b_sync = 0;
301             continue;
302         }
303
304         if( ( p_ac3thread->p_aout_fifo != NULL ) &&
305             ( p_ac3thread->p_aout_fifo->i_rate != sync_info.sample_rate ) )
306         {
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));
312
313             p_ac3thread->p_aout_fifo = NULL;
314         }
315
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 )
321             {
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
338
339                 free( p_ac3thread->ac3_decoder->samples_orig );
340         
341                 module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
342                 module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
343         
344                 free( p_ac3thread->ac3_decoder->imdct_orig );
345                 free( p_ac3thread->ac3_decoder_orig );
346         
347                 return( -1 );
348             }
349         }
350
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] )
355         {
356             p_ac3thread->p_aout_fifo->date[
357                 p_ac3thread->p_aout_fifo->i_end_frame] =
358                 LAST_MDATE;
359         }
360     
361         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
362             (p_ac3thread->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
363
364         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
365         {
366             b_sync = 0;
367             continue;
368         }
369         
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);
375
376         RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
377     }
378
379     /* If b_error is set, the ac3 decoder thread enters the error loop */
380     if (p_ac3thread->p_fifo->b_error)
381     {
382         DecoderError( p_ac3thread->p_fifo );
383     }
384
385     /* End of the ac3 decoder thread */
386     EndThread (p_ac3thread);
387
388     free( p_ac3thread_orig );
389
390     return( 0 );
391 }
392
393
394 /*****************************************************************************
395  * EndThread : ac3 decoder thread destruction
396  *****************************************************************************/
397 static void EndThread (ac3dec_thread_t * p_ac3thread)
398 {
399     /* If the audio output fifo was created, we destroy it */
400     if (p_ac3thread->p_aout_fifo != NULL)
401     {
402         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
403
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));
408     }
409
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
428
429     free( p_ac3thread->ac3_decoder->samples_orig );
430
431     /* Unlock the modules */
432     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
433     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
434
435     /* Free what's left of the decoder */
436     free( p_ac3thread->ac3_decoder->imdct_orig );
437     free( p_ac3thread->ac3_decoder_orig );
438 }
439
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 )
447 {
448     if( b_new_pes )
449     {
450         /* Drop special AC3 header */
451         p_bit_stream->p_byte += 3;
452     }
453 }
454