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