ALL : In order to make ac3 working in avi file, I've deplaced some code
[vlc.git] / 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.31 2002/05/27 16:01:42 fenrir 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_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
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
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 ) );
126
127     /*
128      * Choose the best downmix module
129      */
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,
133                                     NULL );
134     if( psz_name ) free( psz_name );
135
136     if( DOWNMIX.p_module == NULL )
137     {
138         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
139         free( p_ac3thread->ac3_decoder_orig );
140         return( -1 );
141     }
142
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;
151 #undef F
152 #undef DOWNMIX
153
154     /*
155      * Choose the best IMDCT module
156      */
157     p_ac3thread->ac3_decoder->imdct =
158     vlc_memalign( &p_ac3thread->ac3_decoder->imdct_orig, 16, sizeof(imdct_t) );
159     
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,
163                                    NULL );
164     if( psz_name ) free( psz_name );
165
166     if( IMDCT->p_module == NULL )
167     {
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 );
172         return( -1 );
173     }
174
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;
181 #undef F
182
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) );
187
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) );
218 #undef IMDCT
219
220     _M( ac3_init )( p_ac3thread->ac3_decoder );
221
222     /*
223      * Initialize the output properties
224      */
225     p_ac3thread->p_aout_fifo = NULL;
226
227     /*
228      * Bit stream
229      */
230     InitBitstream(&p_ac3thread->ac3_decoder->bit_stream,
231             p_ac3thread->p_config->p_decoder_fifo,
232             BitstreamCallback, (void *) p_ac3thread );
233     
234     return( 0 );
235 }
236
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 )
241 {
242     ac3dec_thread_t *   p_ac3thread;
243     void *              p_ac3thread_orig;         /* pointer before memalign */
244     boolean_t           b_sync = 0;
245
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) );
249
250     if( p_ac3thread == NULL )
251     {
252         intf_ErrMsg ( "ac3_adec error: not enough memory "
253                       "for decoder_Run() to allocate p_ac3thread" );
254         DecoderError( p_config->p_decoder_fifo );
255         return( -1 );
256     }
257
258     /*
259      * Initialize the thread properties
260      */
261     p_ac3thread->p_config = p_config;
262     if( InitThread( p_ac3thread ) )
263     {
264         intf_ErrMsg( "ac3_adec error: could not initialize thread" );
265         DecoderError( p_config->p_decoder_fifo );
266         free( p_ac3thread_orig );
267         return( -1 );
268     }
269
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))
273     {
274         s16 * buffer;
275         ac3_sync_info_t sync_info;
276
277         if( !b_sync )
278         {
279              int i_sync_ptr;
280 #define p_bit_stream (&p_ac3thread->ac3_decoder->bit_stream)
281
282              /* Go to the next PES packet and jump to sync_ptr */
283              do {
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;
292
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 );
297              b_sync = 1;
298 #undef p_bit_stream
299         }
300
301         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
302         {
303             b_sync = 0;
304             continue;
305         }
306
307         if( ( p_ac3thread->p_aout_fifo != NULL ) &&
308             ( p_ac3thread->p_aout_fifo->i_rate != sync_info.sample_rate ) )
309         {
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));
315
316             p_ac3thread->p_aout_fifo = NULL;
317         }
318
319         /* Creating the audio output fifo if not created yet */
320         if (p_ac3thread->p_aout_fifo == NULL )
321         {
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 )
325             {
326                 p_ac3thread->p_fifo->b_error = 1;
327                 break;
328             }
329         }
330
331         CurrentPTS( &p_ac3thread->ac3_decoder->bit_stream,
332             &p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame],
333             NULL );
334         if( !p_ac3thread->p_aout_fifo->date[p_ac3thread->p_aout_fifo->i_end_frame] )
335         {
336             p_ac3thread->p_aout_fifo->date[
337                 p_ac3thread->p_aout_fifo->i_end_frame] =
338                 LAST_MDATE;
339         }
340     
341         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
342             (p_ac3thread->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE);
343
344         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
345         {
346             b_sync = 0;
347             continue;
348         }
349         
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);
355
356         RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
357     }
358
359     /* If b_error is set, the ac3 decoder thread enters the error loop */
360     if (p_ac3thread->p_fifo->b_error)
361     {
362         DecoderError( p_ac3thread->p_fifo );
363     }
364
365     /* End of the ac3 decoder thread */
366     EndThread (p_ac3thread);
367
368     free( p_ac3thread_orig );
369
370     return( 0 );
371 }
372
373
374 /*****************************************************************************
375  * EndThread : ac3 decoder thread destruction
376  *****************************************************************************/
377 static void EndThread (ac3dec_thread_t * p_ac3thread)
378 {
379     /* If the audio output fifo was created, we destroy it */
380     if (p_ac3thread->p_aout_fifo != NULL)
381     {
382         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
383
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));
388     }
389
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 );
407 #undef IMDCT
408
409     free( p_ac3thread->ac3_decoder->samples_orig );
410
411     /* Unlock the modules */
412     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
413     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
414
415     /* Free what's left of the decoder */
416     free( p_ac3thread->ac3_decoder->imdct_orig );
417     free( p_ac3thread->ac3_decoder_orig );
418 }
419
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 )
427 {
428     if( b_new_pes )
429     {
430         /* Drop special AC3 header */
431 /*        p_bit_stream->p_byte += 3; */
432     }
433 }
434