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