]> git.sesse.net Git - vlc/blob - plugins/a52/a52.c
* Backported my two previous commits.
[vlc] / plugins / a52 / a52.c
1 /*****************************************************************************
2  * a52.c: ATSC A/52 aka AC-3 decoder plugin for vlc.
3  *   This plugin makes use of liba52 to decode A/52 audio
4  *   (http://liba52.sf.net/).
5  *****************************************************************************
6  * Copyright (C) 2001 VideoLAN
7  * $Id: a52.c,v 1.14.2.1 2002/05/31 21:57:51 massiot Exp $
8  *
9  * Authors: Gildas Bazin <gbazin@netcourrier.com>
10  *      
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <string.h>                                              /* strdup() */
31 #include <inttypes.h>                                          /* int16_t .. */
32
33 #include <videolan/vlc.h>
34
35 #include "audio_output.h"
36
37 #include "stream_control.h"
38 #include "input_ext-dec.h"
39
40 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
41 #include "include/a52.h"
42 #else
43 #include "a52dec/a52.h"
44 #endif
45
46 #include "a52.h"
47
48 #define AC3DEC_FRAME_SIZE 1536 
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  decoder_Probe  ( u8 * );
54 static int  decoder_Run    ( decoder_config_t * );
55 static int  DecodeFrame    ( a52_adec_thread_t * );
56 static int  InitThread     ( a52_adec_thread_t * );
57 static void EndThread      ( a52_adec_thread_t * );
58
59 static void               BitstreamCallback ( bit_stream_t *, boolean_t );
60 static void               float2s16_2       ( float *, int16_t * );
61 static inline int16_t     convert   ( int32_t );
62
63 /*****************************************************************************
64  * Capabilities
65  *****************************************************************************/
66 void _M( adec_getfunctions )( function_list_t * p_function_list )
67 {
68     p_function_list->functions.dec.pf_probe = decoder_Probe;
69     p_function_list->functions.dec.pf_run   = decoder_Run;
70 }
71
72 /*****************************************************************************
73  * Build configuration structure.
74  *****************************************************************************/
75 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
76 #define DYNRNG_LONGTEXT N_( \
77     "Dynamic range compression makes the loud sounds softer, and the soft " \
78     "sounds louder, so you can more easily listen to the stream in a noisy " \
79     "environment without disturbing anyone.\nIf you disable the dynamic range"\
80     " compression the playback will be more adapted to a movie theater or a " \
81     "listening room.")
82
83 MODULE_CONFIG_START
84 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
85 ADD_BOOL    ( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT )
86 MODULE_CONFIG_STOP
87
88 MODULE_INIT_START
89     SET_DESCRIPTION( _("a52 ATSC A/52 aka AC-3 audio decoder module") )
90     ADD_CAPABILITY( DECODER, 40 )
91     ADD_SHORTCUT( "a52" )
92 MODULE_INIT_STOP
93
94 MODULE_ACTIVATE_START
95     _M( adec_getfunctions )( &p_module->p_functions->dec );
96 MODULE_ACTIVATE_STOP
97
98 MODULE_DEACTIVATE_START
99 MODULE_DEACTIVATE_STOP
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 choose.
106  *****************************************************************************/
107 static int decoder_Probe( u8 *pi_type )
108 {
109     return ( *pi_type == AC3_AUDIO_ES ? 0 : -1 );
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     a52_adec_thread_t *p_a52_adec;
118
119     /* Allocate the memory needed to store the thread's structure */
120     p_a52_adec =
121         (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
122     if (p_a52_adec == NULL)
123     {
124         intf_ErrMsg ( "a52 error: not enough memory "
125                       "for decoder_Run() to allocate p_a52_adec" );
126         DecoderError( p_config->p_decoder_fifo );
127         return( -1 );
128     }
129
130     /* FIXME */
131     p_a52_adec->i_channels = 2;
132
133     /*
134      * Initialize the thread properties
135      */
136     p_a52_adec->p_aout_fifo = NULL;
137     p_a52_adec->p_config = p_config;
138     p_a52_adec->p_fifo = p_a52_adec->p_config->p_decoder_fifo;
139     if( InitThread( p_a52_adec ) )
140     {
141         intf_ErrMsg( "a52 error: could not initialize thread" );
142         DecoderError( p_config->p_decoder_fifo );
143         free( p_a52_adec );
144         return( -1 );
145     }
146
147     /* liba52 decoder thread's main loop */
148     while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
149     {
150
151         /* look for sync word - should be 0x0b77 */
152         RealignBits(&p_a52_adec->bit_stream);
153         while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 && 
154                (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
155         {
156             RemoveBits( &p_a52_adec->bit_stream, 8 );
157         }
158
159         /* get a52 frame header */
160         GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
161         if( p_a52_adec->p_fifo->b_die ) break;
162
163         /* check if frame is valid and get frame info */
164         p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
165                                                &p_a52_adec->flags,
166                                                &p_a52_adec->sample_rate,
167                                                &p_a52_adec->bit_rate );
168
169         if( !p_a52_adec->frame_size )
170         {
171             intf_WarnMsg( 3, "a52: a52_syncinfo failed" );
172             continue;
173         }
174
175         if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
176         {
177             DecoderError( p_config->p_decoder_fifo );
178             free( p_a52_adec );
179             return( -1 );
180         }
181
182     }
183
184     /* If b_error is set, the decoder thread enters the error loop */
185     if( p_a52_adec->p_fifo->b_error )
186     {
187         DecoderError( p_a52_adec->p_fifo );
188     }
189
190     /* End of the liba52 decoder thread */
191     EndThread( p_a52_adec );
192
193     return( 0 );
194 }
195
196 /*****************************************************************************
197  * InitThread: initialize data before entering main loop
198  *****************************************************************************/
199 static int InitThread( a52_adec_thread_t * p_a52_adec )
200 {
201     intf_WarnMsg( 3, "a52: InitThread" );
202
203     /* Initialize liba52 */
204     p_a52_adec->p_a52_state = a52_init( 0 );
205     if( p_a52_adec->p_a52_state == NULL )
206     {
207         intf_ErrMsg ( "a52 error: InitThread() unable to initialize "
208                       "liba52" );
209         return -1;
210     }
211
212     p_a52_adec->b_dynrng = config_GetIntVariable( "a52-dynrng" );
213
214     /* Init the BitStream */
215     InitBitstream( &p_a52_adec->bit_stream,
216                    p_a52_adec->p_fifo,
217                    BitstreamCallback, NULL );
218
219     return( 0 );
220 }
221
222 /*****************************************************************************
223  * DecodeFrame: decodes an ATSC A/52 frame.
224  *****************************************************************************/
225 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
226 {
227     sample_t sample_level = 1;
228     byte_t   *p_buffer;
229     int i;
230
231     if( ( p_a52_adec->p_aout_fifo != NULL ) &&
232         ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
233     {
234         /* Make sure the output thread leaves the NextFrame() function */
235         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
236         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
237         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
238         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
239
240         p_a52_adec->p_aout_fifo = NULL;
241     }
242
243     /* Creating the audio output fifo if not created yet */
244     if( p_a52_adec->p_aout_fifo == NULL )
245     {
246         p_a52_adec->p_aout_fifo = aout_CreateFifo( AOUT_FIFO_PCM,
247                                     p_a52_adec->i_channels,
248                                     p_a52_adec->sample_rate,
249                                     AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
250                                     NULL );
251
252         if ( p_a52_adec->p_aout_fifo == NULL )
253         { 
254             return( -1 );
255         }
256     }
257
258     /* Set the Presentation Time Stamp */
259     CurrentPTS( &p_a52_adec->bit_stream,
260                 &p_a52_adec->p_aout_fifo->date[
261                     p_a52_adec->p_aout_fifo->i_end_frame],
262                 NULL );
263
264     if( !p_a52_adec->p_aout_fifo->date[
265             p_a52_adec->p_aout_fifo->i_end_frame] )
266     {
267         p_a52_adec->p_aout_fifo->date[
268             p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
269     }
270
271
272
273     p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
274         ( p_a52_adec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE *
275           p_a52_adec->i_channels * sizeof(s16) );
276
277     /* FIXME */
278     p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
279
280     /* Get the complete frame */
281     GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
282               p_a52_adec->frame_size - 7 );
283     if( p_a52_adec->p_fifo->b_die ) return( -1 );
284
285     /* do the actual decoding now */
286     a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
287                &p_a52_adec->flags, &sample_level, 384 );
288
289     if( !p_a52_adec->b_dynrng )
290         a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
291
292     for( i = 0; i < 6; i++ )
293     {
294         if( a52_block( p_a52_adec->p_a52_state ) )
295             intf_WarnMsg( 5, "a52: a52_block failed for block %i", i );
296
297         float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
298                      ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
299     }
300
301
302     vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
303     p_a52_adec->p_aout_fifo->i_end_frame = 
304       (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
305     vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
306     vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
307
308     return 0;
309 }
310
311 /*****************************************************************************
312  * EndThread : liba52 decoder thread destruction
313  *****************************************************************************/
314 static void EndThread (a52_adec_thread_t *p_a52_adec)
315 {
316     intf_WarnMsg ( 3, "a52: EndThread" );
317
318     /* If the audio output fifo was created, we destroy it */
319     if (p_a52_adec->p_aout_fifo != NULL)
320     {
321         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
322
323         /* Make sure the output thread leaves the NextFrame() function */
324         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
325         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
326         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
327     }
328
329     a52_free( p_a52_adec->p_a52_state );
330     free( p_a52_adec );
331
332 }
333
334 /*****************************************************************************
335  * float2s16_2 : converts floats to ints using a trick based on the IEEE
336  *               floating-point format
337  *****************************************************************************/
338 static inline int16_t convert (int32_t i)
339 {
340     if (i > 0x43c07fff)
341         return 32767;
342     else if (i < 0x43bf8000)
343         return -32768;
344     else
345         return i - 0x43c00000;
346 }
347
348 static void float2s16_2 (float * _f, int16_t * s16)
349 {
350     int i;
351     int32_t * f = (int32_t *) _f;
352
353     for (i = 0; i < 256; i++) {
354       s16[2*i] = convert (f[i]);
355         s16[2*i+1] = convert (f[i+256]);
356     }
357 }
358
359 /*****************************************************************************
360  * BitstreamCallback: Import parameters from the new data/PES packet
361  *****************************************************************************
362  * This function is called by input's NextDataPacket.
363  *****************************************************************************/
364 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
365                                 boolean_t b_new_pes )
366 {
367     if( b_new_pes )
368     {
369         /* Drop special AC3 header */
370 /*        p_bit_stream->p_byte += 3; */
371     }
372 }