]> git.sesse.net Git - vlc/blob - plugins/ac3_adec/ac3_adec.c
STABLE/HEAD merge ; backported the Next Generation Buffer Manager.
[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.5.2.2 2001/12/31 01:21:44 massiot 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 #define MODULE_NAME ac3_adec
25 #include "modules_inner.h"
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include "defs.h"
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>                                              /* getpid() */
34 #endif
35
36 #include <stdlib.h>                                      /* malloc(), free() */
37 #include <string.h>                                              /* memset() */
38
39 #include "config.h"
40 #include "common.h"
41 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
42 #include "threads.h"
43 #include "mtime.h"
44
45 #include "audio_output.h"
46
47 #include "modules.h"
48 #include "modules_export.h"
49
50 #include "stream_control.h"
51 #include "input_ext-dec.h"
52 #include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
53
54 #include "ac3_imdct.h"
55 #include "ac3_downmix.h"
56 #include "ac3_decoder.h"
57 #include "ac3_adec.h"
58
59 #define AC3DEC_FRAME_SIZE (2*1536) 
60
61 /*****************************************************************************
62  * Local prototypes
63  *****************************************************************************/
64 static int  decoder_Probe     ( probedata_t * );
65 static int  decoder_Run       ( decoder_config_t * );
66 static int  InitThread        ( ac3dec_thread_t * p_adec );
67 static void EndThread         ( ac3dec_thread_t * p_adec );
68 static void BitstreamCallback ( bit_stream_t *p_bit_stream,
69                                 boolean_t b_new_pes );
70
71 /*****************************************************************************
72  * Capabilities
73  *****************************************************************************/
74 void _M( adec_getfunctions )( function_list_t * p_function_list )
75 {
76     p_function_list->pf_probe = decoder_Probe;
77     p_function_list->functions.dec.pf_run = decoder_Run;
78 }
79
80 /*****************************************************************************
81  * Build configuration tree.
82  *****************************************************************************/
83 MODULE_CONFIG_START
84 ADD_WINDOW( "Configuration for ac3 decoder module" )
85     ADD_COMMENT( "Nothing to configure" )
86 MODULE_CONFIG_STOP
87
88 MODULE_INIT_START
89     p_module->i_capabilities = MODULE_CAPABILITY_DEC;
90     p_module->psz_longname = "Ac3 sofware decoder";
91 MODULE_INIT_STOP
92
93 MODULE_ACTIVATE_START
94     _M( adec_getfunctions )( &p_module->p_functions->dec );
95 MODULE_ACTIVATE_STOP
96
97 MODULE_DEACTIVATE_START
98 MODULE_DEACTIVATE_STOP
99
100
101 /*****************************************************************************
102  * decoder_Probe: probe the decoder and return score
103  *****************************************************************************
104  * Tries to launch a decoder and return score so that the interface is able 
105  * to chose.
106  *****************************************************************************/
107 static int decoder_Probe( probedata_t *p_data )
108 {
109     return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
110 }
111
112 /*****************************************************************************
113  * decoder_Run: this function is called just after the thread is created
114  *****************************************************************************/
115 static int decoder_Run ( decoder_config_t * p_config )
116 {
117     ac3dec_thread_t *   p_ac3thread;
118     int sync;
119
120     intf_DbgMsg( "ac3_adec debug: ac3_adec thread launched, initializing" );
121
122     /* Allocate the memory needed to store the thread's structure */
123     p_ac3thread = (ac3dec_thread_t *)memalign(16, sizeof(ac3dec_thread_t));
124
125     if( p_ac3thread == NULL )
126     {
127         intf_ErrMsg ( "ac3_adec error: not enough memory "
128                       "for decoder_Run() to allocate p_ac3thread" );
129         DecoderError( p_config->p_decoder_fifo );
130         return( -1 );
131     }
132    
133     /*
134      * Initialize the thread properties
135      */
136     p_ac3thread->p_config = p_config;
137     if( InitThread( p_ac3thread ) )
138     {
139         intf_ErrMsg( "ac3_adec error: could not initialize thread" );
140         DecoderError( p_config->p_decoder_fifo );
141         free( p_ac3thread );
142         return( -1 );
143     }
144
145     sync = 0;
146     p_ac3thread->sync_ptr = 0;
147
148     /* ac3 decoder thread's main loop */
149     /* FIXME : do we have enough room to store the decoded frames ?? */
150     while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
151     {
152         s16 * buffer;
153         ac3_sync_info_t sync_info;
154         int ptr;
155
156         if (!sync) {
157             do {
158                 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
159             } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
160                     && (!p_ac3thread->p_fifo->b_error));
161             
162             ptr = p_ac3thread->sync_ptr;
163
164             while(ptr-- && (!p_ac3thread->p_fifo->b_die)
165                 && (!p_ac3thread->p_fifo->b_error))
166             {
167                 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
168             }
169                         
170             /* we are in sync now */
171             sync = 1;
172         }
173
174         if (p_ac3thread->p_fifo->p_first->i_pts)
175         {
176             p_ac3thread->p_aout_fifo->date[
177                 p_ac3thread->p_aout_fifo->l_end_frame] =
178                 p_ac3thread->p_fifo->p_first->i_pts;
179             p_ac3thread->p_fifo->p_first->i_pts = 0;
180         } else {
181             p_ac3thread->p_aout_fifo->date[
182                 p_ac3thread->p_aout_fifo->l_end_frame] =
183                 LAST_MDATE;
184         }
185     
186         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
187         {
188             sync = 0;
189             goto bad_frame;
190         }
191
192         p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
193
194         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
195             (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
196
197         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
198         {
199             sync = 0;
200             goto bad_frame;
201         }
202         
203         vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
204         p_ac3thread->p_aout_fifo->l_end_frame = 
205             (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
206         vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
207         vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
208
209         bad_frame:
210             RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
211     }
212
213     /* If b_error is set, the ac3 decoder thread enters the error loop */
214     if (p_ac3thread->p_fifo->b_error)
215     {
216         DecoderError( p_ac3thread->p_fifo );
217     }
218
219     /* End of the ac3 decoder thread */
220     EndThread (p_ac3thread);
221
222     free( p_ac3thread );
223
224     return( 0 );
225 }
226
227
228 /*****************************************************************************
229  * InitThread: initialize data before entering main loop
230  *****************************************************************************/
231 static int InitThread( ac3dec_thread_t * p_ac3thread )
232 {
233     /*
234      * Thread properties 
235      */
236     p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
237     p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
238
239     /*
240      * Choose the best downmix module
241      */
242 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
243     DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
244
245     if( DOWNMIX.p_module == NULL )
246     {
247         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
248         free( p_ac3thread->ac3_decoder );
249         return( -1 );
250     }
251
252 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
253     DOWNMIX.pf_downmix_3f_2r_to_2ch     = F.pf_downmix_3f_2r_to_2ch;
254     DOWNMIX.pf_downmix_2f_2r_to_2ch     = F.pf_downmix_2f_2r_to_2ch;
255     DOWNMIX.pf_downmix_3f_1r_to_2ch     = F.pf_downmix_3f_1r_to_2ch;
256     DOWNMIX.pf_downmix_2f_1r_to_2ch     = F.pf_downmix_2f_1r_to_2ch;
257     DOWNMIX.pf_downmix_3f_0r_to_2ch     = F.pf_downmix_3f_0r_to_2ch;
258     DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
259     DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
260 #undef F
261 #undef DOWNMIX
262
263     /*
264      * Choose the best IMDCT module
265      */
266     p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
267     
268 #define IMDCT p_ac3thread->ac3_decoder->imdct
269     IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
270
271     if( IMDCT->p_module == NULL )
272     {
273         intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
274         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
275         free( p_ac3thread->ac3_decoder->imdct );
276         free( p_ac3thread->ac3_decoder );
277         return( -1 );
278     }
279
280 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
281     IMDCT->pf_imdct_init    = F.pf_imdct_init;
282     IMDCT->pf_imdct_256     = F.pf_imdct_256;
283     IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
284     IMDCT->pf_imdct_512     = F.pf_imdct_512;
285     IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
286 #undef F
287
288     /* Initialize the ac3 decoder structures */
289 #define p_dec p_ac3thread->ac3_decoder
290 #if defined( __MINGW32__ )
291     p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
292     p_dec->samples = (float *)
293                      (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
294 #else
295     p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
296 #endif
297 #undef p_dec
298
299     IMDCT->buf    = memalign( 16, N/4 * sizeof(complex_t) );
300     IMDCT->delay  = memalign( 16, 6 * 256 * sizeof(float) );
301     IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
302     IMDCT->xcos1  = memalign( 16, N/4 * sizeof(float) );
303     IMDCT->xsin1  = memalign( 16, N/4 * sizeof(float) );
304     IMDCT->xcos2  = memalign( 16, N/8 * sizeof(float) );
305     IMDCT->xsin2  = memalign( 16, N/8 * sizeof(float) );
306     IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
307     IMDCT->w_1    = memalign( 16, 1  * sizeof(complex_t) );
308     IMDCT->w_2    = memalign( 16, 2  * sizeof(complex_t) );
309     IMDCT->w_4    = memalign( 16, 4  * sizeof(complex_t) );
310     IMDCT->w_8    = memalign( 16, 8  * sizeof(complex_t) );
311     IMDCT->w_16   = memalign( 16, 16 * sizeof(complex_t) );
312     IMDCT->w_32   = memalign( 16, 32 * sizeof(complex_t) );
313     IMDCT->w_64   = memalign( 16, 64 * sizeof(complex_t) );
314
315     ac3_init( p_ac3thread->ac3_decoder );
316
317     /*
318      * Initialize the output properties
319      */
320     p_ac3thread->p_aout_fifo = NULL;
321
322     intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized", 
323                   p_ac3thread );
324
325     /*
326      * Bit stream
327      */
328     p_ac3thread->p_config->pf_init_bit_stream(
329             &p_ac3thread->ac3_decoder->bit_stream,
330             p_ac3thread->p_config->p_decoder_fifo,
331             BitstreamCallback, (void *) p_ac3thread );
332     
333     /* Creating the audio output fifo */
334     p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
335                                                AC3DEC_FRAME_SIZE, NULL  );
336     if ( p_ac3thread->p_aout_fifo == NULL )
337     {
338         free( IMDCT->w_1 );
339         free( IMDCT->w_64 );
340         free( IMDCT->w_32 );
341         free( IMDCT->w_16 );
342         free( IMDCT->w_8 );
343         free( IMDCT->w_4 );
344         free( IMDCT->w_2 );
345         free( IMDCT->xcos_sin_sse );
346         free( IMDCT->xsin2 );
347         free( IMDCT->xcos2 );
348         free( IMDCT->xsin1 );
349         free( IMDCT->xcos1 );
350         free( IMDCT->delay1 );
351         free( IMDCT->delay );
352         free( IMDCT->buf );
353 #undef IMDCT
354
355 #if defined( __MINGW32__ )
356         free( p_ac3thread->ac3_decoder->samples_back );
357 #else
358         free( p_ac3thread->ac3_decoder->samples );
359 #endif
360
361         module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
362         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
363
364         free( p_ac3thread->ac3_decoder->imdct );
365         free( p_ac3thread->ac3_decoder );
366
367         return( -1 );
368     }
369
370     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
371     
372     return( 0 );
373 }
374
375 /*****************************************************************************
376  * EndThread : ac3 decoder thread destruction
377  *****************************************************************************/
378 static void EndThread (ac3dec_thread_t * p_ac3thread)
379 {
380     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
381
382     /* If the audio output fifo was created, we destroy it */
383     if (p_ac3thread->p_aout_fifo != NULL)
384     {
385         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
386
387         /* Make sure the output thread leaves the NextFrame() function */
388         vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
389         vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
390         vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
391     }
392
393     /* Free allocated structures */
394 #define IMDCT p_ac3thread->ac3_decoder->imdct
395     free( IMDCT->w_1 );
396     free( IMDCT->w_64 );
397     free( IMDCT->w_32 );
398     free( IMDCT->w_16 );
399     free( IMDCT->w_8 );
400     free( IMDCT->w_4 );
401     free( IMDCT->w_2 );
402     free( IMDCT->xcos_sin_sse );
403     free( IMDCT->xsin2 );
404     free( IMDCT->xcos2 );
405     free( IMDCT->xsin1 );
406     free( IMDCT->xcos1 );
407     free( IMDCT->delay1 );
408     free( IMDCT->delay );
409     free( IMDCT->buf );
410 #undef IMDCT
411
412 #if defined( __MINGW32__ )
413     free( p_ac3thread->ac3_decoder->samples_back );
414 #else
415     free( p_ac3thread->ac3_decoder->samples );
416 #endif
417
418     /* Unlock the modules */
419     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
420     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
421
422     /* Free what's left of the decoder */
423     free( p_ac3thread->ac3_decoder->imdct );
424     free( p_ac3thread->ac3_decoder );
425
426     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
427 }
428
429 /*****************************************************************************
430  * BitstreamCallback: Import parameters from the new data/PES packet
431  *****************************************************************************
432  * This function is called by input's NextDataPacket.
433  *****************************************************************************/
434 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
435                                         boolean_t b_new_pes)
436 {
437
438     ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
439
440     if( b_new_pes )
441     {
442         int ptr;
443         
444         ptr = *(p_bit_stream->p_byte + 1);
445         ptr <<= 8;
446         ptr |= *(p_bit_stream->p_byte + 2);
447         p_ac3thread->sync_ptr = ptr;
448         p_bit_stream->p_byte += 3;                                                            
449     }
450 }
451