]> git.sesse.net Git - vlc/blob - plugins/ac3_adec/ac3_adec.c
* COMPLETE CVS BREAKAGE !! The MAIN branch is going to be a playground
[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.6 2001/12/09 17:01:36 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 #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 "common.h"
40 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
41 #include "threads.h"
42 #include "mtime.h"
43
44 #include "audio_output.h"
45
46 #include "modules.h"
47 #include "modules_export.h"
48
49 #include "stream_control.h"
50 #include "input_ext-dec.h"
51 #include "input_ext-intf.h"                                /* MPEG?_AUDIO_ES */
52
53 #include "ac3_imdct.h"
54 #include "ac3_downmix.h"
55 #include "ac3_decoder.h"
56 #include "ac3_adec.h"
57
58 #define AC3DEC_FRAME_SIZE (2*1536) 
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63 static int      ac3_adec_Probe      ( probedata_t * );
64 static int      ac3_adec_Run         ( decoder_config_t * );
65 static int      ac3_adec_Init        (ac3dec_thread_t * p_adec);
66 static void     ac3_adec_ErrorThread (ac3dec_thread_t * p_adec);
67 static void     ac3_adec_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 = ac3_adec_Probe;
77     p_function_list->functions.dec.pf_run = ac3_adec_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  * ac3_adec_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 ac3_adec_Probe( probedata_t *p_data )
108 {
109     return ( p_data->i_type == AC3_AUDIO_ES ) ? 50 : 0;
110 }
111
112 /*****************************************************************************
113  * ac3_adec_Run: this function is called just after the thread is created
114  *****************************************************************************/
115 static int ac3_adec_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 ac3_adec_Run() to allocate p_ac3thread" );
129         return( -1 );
130     }
131    
132     /*
133      * Initialize the thread properties
134      */
135     p_ac3thread->p_config = p_config;
136     if( ac3_adec_Init( p_ac3thread ) )
137     {
138         intf_ErrMsg( "ac3_adec error: could not initialize thread" );
139         free( p_ac3thread );
140         return( -1 );
141     }
142
143     sync = 0;
144     p_ac3thread->sync_ptr = 0;
145
146     /* ac3 decoder thread's main loop */
147     /* FIXME : do we have enough room to store the decoded frames ?? */
148     while ((!p_ac3thread->p_fifo->b_die) && (!p_ac3thread->p_fifo->b_error))
149     {
150         s16 * buffer;
151         ac3_sync_info_t sync_info;
152         int ptr;
153
154         if (!sync) {
155             do {
156                 GetBits(&p_ac3thread->ac3_decoder->bit_stream,8);
157             } while ((!p_ac3thread->sync_ptr) && (!p_ac3thread->p_fifo->b_die)
158                     && (!p_ac3thread->p_fifo->b_error));
159             
160             ptr = p_ac3thread->sync_ptr;
161
162             while(ptr-- && (!p_ac3thread->p_fifo->b_die)
163                 && (!p_ac3thread->p_fifo->b_error))
164             {
165                 p_ac3thread->ac3_decoder->bit_stream.p_byte++;
166             }
167                         
168             /* we are in sync now */
169             sync = 1;
170         }
171
172         if (DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts)
173         {
174             p_ac3thread->p_aout_fifo->date[
175                 p_ac3thread->p_aout_fifo->l_end_frame] =
176                 DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts;
177             DECODER_FIFO_START(*p_ac3thread->p_fifo)->i_pts = 0;
178         } else {
179             p_ac3thread->p_aout_fifo->date[
180                 p_ac3thread->p_aout_fifo->l_end_frame] =
181                 LAST_MDATE;
182         }
183     
184         if (ac3_sync_frame (p_ac3thread->ac3_decoder, &sync_info))
185         {
186             sync = 0;
187             goto bad_frame;
188         }
189
190         p_ac3thread->p_aout_fifo->l_rate = sync_info.sample_rate;
191
192         buffer = ((s16 *)p_ac3thread->p_aout_fifo->buffer) + 
193             (p_ac3thread->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE);
194
195         if (ac3_decode_frame (p_ac3thread->ac3_decoder, buffer))
196         {
197             sync = 0;
198             goto bad_frame;
199         }
200         
201         vlc_mutex_lock (&p_ac3thread->p_aout_fifo->data_lock);
202         p_ac3thread->p_aout_fifo->l_end_frame = 
203             (p_ac3thread->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
204         vlc_cond_signal (&p_ac3thread->p_aout_fifo->data_wait);
205         vlc_mutex_unlock (&p_ac3thread->p_aout_fifo->data_lock);
206
207         bad_frame:
208             RealignBits(&p_ac3thread->ac3_decoder->bit_stream);
209     }
210
211     /* If b_error is set, the ac3 decoder thread enters the error loop */
212     if (p_ac3thread->p_fifo->b_error)
213     {
214         ac3_adec_ErrorThread (p_ac3thread);
215     }
216
217     /* End of the ac3 decoder thread */
218     ac3_adec_EndThread (p_ac3thread);
219
220     free( p_ac3thread );
221
222     return( 0 );
223 }
224
225
226 /*****************************************************************************
227  * ac3_adec_Init: initialize data before entering main loop
228  *****************************************************************************/
229 static int ac3_adec_Init( ac3dec_thread_t * p_ac3thread )
230 {
231     /*
232      * Thread properties 
233      */
234     p_ac3thread->p_fifo = p_ac3thread->p_config->p_decoder_fifo;
235     p_ac3thread->ac3_decoder = memalign( 16, sizeof(ac3dec_t) );
236
237     /*
238      * Choose the best downmix module
239      */
240 #define DOWNMIX p_ac3thread->ac3_decoder->downmix
241     DOWNMIX.p_module = module_Need( MODULE_CAPABILITY_DOWNMIX, NULL );
242
243     if( DOWNMIX.p_module == NULL )
244     {
245         intf_ErrMsg( "ac3dec error: no suitable downmix module" );
246         free( p_ac3thread->ac3_decoder );
247         return( -1 );
248     }
249
250 #define F DOWNMIX.p_module->p_functions->downmix.functions.downmix
251     DOWNMIX.pf_downmix_3f_2r_to_2ch     = F.pf_downmix_3f_2r_to_2ch;
252     DOWNMIX.pf_downmix_2f_2r_to_2ch     = F.pf_downmix_2f_2r_to_2ch;
253     DOWNMIX.pf_downmix_3f_1r_to_2ch     = F.pf_downmix_3f_1r_to_2ch;
254     DOWNMIX.pf_downmix_2f_1r_to_2ch     = F.pf_downmix_2f_1r_to_2ch;
255     DOWNMIX.pf_downmix_3f_0r_to_2ch     = F.pf_downmix_3f_0r_to_2ch;
256     DOWNMIX.pf_stream_sample_2ch_to_s16 = F.pf_stream_sample_2ch_to_s16;
257     DOWNMIX.pf_stream_sample_1ch_to_s16 = F.pf_stream_sample_1ch_to_s16;
258 #undef F
259 #undef DOWNMIX
260
261     /*
262      * Choose the best IMDCT module
263      */
264     p_ac3thread->ac3_decoder->imdct = memalign(16, sizeof(imdct_t));
265     
266 #define IMDCT p_ac3thread->ac3_decoder->imdct
267     IMDCT->p_module = module_Need( MODULE_CAPABILITY_IMDCT, NULL );
268
269     if( IMDCT->p_module == NULL )
270     {
271         intf_ErrMsg( "ac3dec error: no suitable IMDCT module" );
272         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
273         free( p_ac3thread->ac3_decoder->imdct );
274         free( p_ac3thread->ac3_decoder );
275         return( -1 );
276     }
277
278 #define F IMDCT->p_module->p_functions->imdct.functions.imdct
279     IMDCT->pf_imdct_init    = F.pf_imdct_init;
280     IMDCT->pf_imdct_256     = F.pf_imdct_256;
281     IMDCT->pf_imdct_256_nol = F.pf_imdct_256_nol;
282     IMDCT->pf_imdct_512     = F.pf_imdct_512;
283     IMDCT->pf_imdct_512_nol = F.pf_imdct_512_nol;
284 #undef F
285
286     /* Initialize the ac3 decoder structures */
287 #define p_dec p_ac3thread->ac3_decoder
288 #if defined( __MINGW32__ )
289     p_dec->samples_back = memalign( 16, 6 * 256 * sizeof(float) + 15 );
290     p_dec->samples = (float *)
291                      (((unsigned long) p_dec->samples_back + 15 ) & ~0xFUL);
292 #else
293     p_dec->samples = memalign( 16, 6 * 256 * sizeof(float) );
294 #endif
295 #undef p_dec
296
297     IMDCT->buf    = memalign( 16, N/4 * sizeof(complex_t) );
298     IMDCT->delay  = memalign( 16, 6 * 256 * sizeof(float) );
299     IMDCT->delay1 = memalign( 16, 6 * 256 * sizeof(float) );
300     IMDCT->xcos1  = memalign( 16, N/4 * sizeof(float) );
301     IMDCT->xsin1  = memalign( 16, N/4 * sizeof(float) );
302     IMDCT->xcos2  = memalign( 16, N/8 * sizeof(float) );
303     IMDCT->xsin2  = memalign( 16, N/8 * sizeof(float) );
304     IMDCT->xcos_sin_sse = memalign( 16, 128 * 4 * sizeof(float) );
305     IMDCT->w_1    = memalign( 16, 1  * sizeof(complex_t) );
306     IMDCT->w_2    = memalign( 16, 2  * sizeof(complex_t) );
307     IMDCT->w_4    = memalign( 16, 4  * sizeof(complex_t) );
308     IMDCT->w_8    = memalign( 16, 8  * sizeof(complex_t) );
309     IMDCT->w_16   = memalign( 16, 16 * sizeof(complex_t) );
310     IMDCT->w_32   = memalign( 16, 32 * sizeof(complex_t) );
311     IMDCT->w_64   = memalign( 16, 64 * sizeof(complex_t) );
312
313     ac3_init( p_ac3thread->ac3_decoder );
314
315     /*
316      * Initialize the output properties
317      */
318     p_ac3thread->p_aout_fifo = NULL;
319
320     intf_DbgMsg ( "ac3_adec debug: ac3_adec thread (%p) initialized", 
321                   p_ac3thread );
322
323     /*
324      * Bit stream
325      */
326     p_ac3thread->p_config->pf_init_bit_stream(
327             &p_ac3thread->ac3_decoder->bit_stream,
328             p_ac3thread->p_config->p_decoder_fifo,
329             BitstreamCallback, (void *) p_ac3thread );
330     
331     /* Creating the audio output fifo */
332     p_ac3thread->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO, 2, 0, 0,
333                                                AC3DEC_FRAME_SIZE, NULL  );
334     if ( p_ac3thread->p_aout_fifo == NULL )
335     {
336         free( IMDCT->w_1 );
337         free( IMDCT->w_64 );
338         free( IMDCT->w_32 );
339         free( IMDCT->w_16 );
340         free( IMDCT->w_8 );
341         free( IMDCT->w_4 );
342         free( IMDCT->w_2 );
343         free( IMDCT->xcos_sin_sse );
344         free( IMDCT->xsin2 );
345         free( IMDCT->xcos2 );
346         free( IMDCT->xsin1 );
347         free( IMDCT->xcos1 );
348         free( IMDCT->delay1 );
349         free( IMDCT->delay );
350         free( IMDCT->buf );
351 #undef IMDCT
352
353 #if defined( __MINGW32__ )
354         free( p_ac3thread->ac3_decoder->samples_back );
355 #else
356         free( p_ac3thread->ac3_decoder->samples );
357 #endif
358
359         module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
360         module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
361
362         free( p_ac3thread->ac3_decoder->imdct );
363         free( p_ac3thread->ac3_decoder );
364
365         return( -1 );
366     }
367
368     intf_DbgMsg("ac3dec debug: ac3 decoder thread %p initialized", p_ac3thread);
369     
370     return( 0 );
371 }
372
373
374 /*****************************************************************************
375  * ac3_adec_ErrorThread : ac3 decoder's RunThread() error loop
376  *****************************************************************************/
377 static void ac3_adec_ErrorThread (ac3dec_thread_t * p_ac3thread)
378 {
379     /* We take the lock, because we are going to read/write the start/end
380      * indexes of the decoder fifo */
381     vlc_mutex_lock (&p_ac3thread->p_fifo->data_lock);
382
383     /* Wait until a `die' order is sent */
384     while (!p_ac3thread->p_fifo->b_die)
385     {
386         /* Trash all received PES packets */
387         while (!DECODER_FIFO_ISEMPTY(*p_ac3thread->p_fifo))
388         {
389             p_ac3thread->p_fifo->pf_delete_pes(
390                     p_ac3thread->p_fifo->p_packets_mgt,
391                     DECODER_FIFO_START(*p_ac3thread->p_fifo));
392             DECODER_FIFO_INCSTART (*p_ac3thread->p_fifo);
393         }
394
395         /* Waiting for the input thread to put new PES packets in the fifo */
396         vlc_cond_wait (&p_ac3thread->p_fifo->data_wait,
397                        &p_ac3thread->p_fifo->data_lock);
398     }
399
400     /* We can release the lock before leaving */
401     vlc_mutex_unlock (&p_ac3thread->p_fifo->data_lock);
402 }
403
404 /*****************************************************************************
405  * ac3_adec_EndThread : ac3 decoder thread destruction
406  *****************************************************************************/
407 static void ac3_adec_EndThread (ac3dec_thread_t * p_ac3thread)
408 {
409     intf_DbgMsg ("ac3dec debug: destroying ac3 decoder thread %p", p_ac3thread);
410
411     /* If the audio output fifo was created, we destroy it */
412     if (p_ac3thread->p_aout_fifo != NULL)
413     {
414         aout_DestroyFifo (p_ac3thread->p_aout_fifo);
415
416         /* Make sure the output thread leaves the NextFrame() function */
417         vlc_mutex_lock (&(p_ac3thread->p_aout_fifo->data_lock));
418         vlc_cond_signal (&(p_ac3thread->p_aout_fifo->data_wait));
419         vlc_mutex_unlock (&(p_ac3thread->p_aout_fifo->data_lock));
420     }
421
422     /* Free allocated structures */
423 #define IMDCT p_ac3thread->ac3_decoder->imdct
424     free( IMDCT->w_1 );
425     free( IMDCT->w_64 );
426     free( IMDCT->w_32 );
427     free( IMDCT->w_16 );
428     free( IMDCT->w_8 );
429     free( IMDCT->w_4 );
430     free( IMDCT->w_2 );
431     free( IMDCT->xcos_sin_sse );
432     free( IMDCT->xsin2 );
433     free( IMDCT->xcos2 );
434     free( IMDCT->xsin1 );
435     free( IMDCT->xcos1 );
436     free( IMDCT->delay1 );
437     free( IMDCT->delay );
438     free( IMDCT->buf );
439 #undef IMDCT
440
441 #if defined( __MINGW32__ )
442     free( p_ac3thread->ac3_decoder->samples_back );
443 #else
444     free( p_ac3thread->ac3_decoder->samples );
445 #endif
446
447     /* Unlock the modules */
448     module_Unneed( p_ac3thread->ac3_decoder->downmix.p_module );
449     module_Unneed( p_ac3thread->ac3_decoder->imdct->p_module );
450
451     /* Free what's left of the decoder */
452     free( p_ac3thread->ac3_decoder->imdct );
453     free( p_ac3thread->ac3_decoder );
454
455     intf_DbgMsg( "ac3dec debug: ac3 decoder thread %p destroyed", p_ac3thread );
456 }
457
458 /*****************************************************************************
459  * BitstreamCallback: Import parameters from the new data/PES packet
460  *****************************************************************************
461  * This function is called by input's NextDataPacket.
462  *****************************************************************************/
463 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
464                                         boolean_t b_new_pes)
465 {
466
467     ac3dec_thread_t *p_ac3thread=(ac3dec_thread_t *)p_bit_stream->p_callback_arg;
468
469     if( b_new_pes )
470     {
471         int ptr;
472         
473         ptr = *(p_bit_stream->p_byte + 1);
474         ptr <<= 8;
475         ptr |= *(p_bit_stream->p_byte + 2);
476         p_ac3thread->sync_ptr = ptr;
477         p_bit_stream->p_byte += 3;                                                            
478     }
479 }
480