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.3 2002/09/25 23:11:51 massiot Exp $
9 * Authors: Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <string.h> /* strdup() */
32 #include <videolan/vlc.h>
35 # include <stdint.h> /* int16_t .. */
37 # include <inttypes.h> /* int16_t .. */
40 #include "audio_output.h"
42 #include "stream_control.h"
43 #include "input_ext-dec.h"
45 #ifdef USE_A52DEC_TREE /* liba52 header file */
46 #include "include/a52.h"
48 #include "a52dec/a52.h"
53 #define AC3DEC_FRAME_SIZE 1536
55 /*****************************************************************************
57 *****************************************************************************/
58 static int decoder_Probe ( u8 * );
59 static int decoder_Run ( decoder_config_t * );
60 static int DecodeFrame ( a52_adec_thread_t * );
61 static int InitThread ( a52_adec_thread_t * );
62 static void EndThread ( a52_adec_thread_t * );
64 static void BitstreamCallback ( bit_stream_t *, boolean_t );
65 static void float2s16_2 ( float *, int16_t * );
66 static inline int16_t convert ( int32_t );
68 /*****************************************************************************
70 *****************************************************************************/
71 void _M( adec_getfunctions )( function_list_t * p_function_list )
73 p_function_list->functions.dec.pf_probe = decoder_Probe;
74 p_function_list->functions.dec.pf_run = decoder_Run;
77 /*****************************************************************************
78 * Build configuration structure.
79 *****************************************************************************/
80 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
81 #define DYNRNG_LONGTEXT N_( \
82 "Dynamic range compression makes the loud sounds softer, and the soft " \
83 "sounds louder, so you can more easily listen to the stream in a noisy " \
84 "environment without disturbing anyone.\nIf you disable the dynamic range"\
85 " compression the playback will be more adapted to a movie theater or a " \
89 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
90 ADD_BOOL ( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT )
94 SET_DESCRIPTION( _("a52 ATSC A/52 aka AC-3 audio decoder module") )
95 ADD_CAPABILITY( DECODER, 40 )
100 _M( adec_getfunctions )( &p_module->p_functions->dec );
103 MODULE_DEACTIVATE_START
104 MODULE_DEACTIVATE_STOP
106 /*****************************************************************************
107 * decoder_Probe: probe the decoder and return score
108 *****************************************************************************
109 * Tries to launch a decoder and return score so that the interface is able
111 *****************************************************************************/
112 static int decoder_Probe( u8 *pi_type )
114 return ( (*pi_type == AC3_AUDIO_ES || *pi_type == A52B_AUDIO_ES) ? 0 : -1 );
117 /*****************************************************************************
118 * decoder_Run: this function is called just after the thread is created
119 *****************************************************************************/
120 static int decoder_Run ( decoder_config_t *p_config )
122 a52_adec_thread_t *p_a52_adec;
124 /* Allocate the memory needed to store the thread's structure */
126 (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
127 if (p_a52_adec == NULL)
129 intf_ErrMsg ( "a52 error: not enough memory "
130 "for decoder_Run() to allocate p_a52_adec" );
131 DecoderError( p_config->p_decoder_fifo );
136 p_a52_adec->i_channels = 2;
139 * Initialize the thread properties
141 p_a52_adec->p_aout_fifo = NULL;
142 p_a52_adec->p_config = p_config;
143 p_a52_adec->p_fifo = p_a52_adec->p_config->p_decoder_fifo;
144 if( InitThread( p_a52_adec ) )
146 intf_ErrMsg( "a52 error: could not initialize thread" );
147 DecoderError( p_config->p_decoder_fifo );
152 /* liba52 decoder thread's main loop */
153 while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
156 /* look for sync word - should be 0x0b77 */
157 RealignBits(&p_a52_adec->bit_stream);
158 while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 &&
159 (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
161 RemoveBits( &p_a52_adec->bit_stream, 8 );
164 /* get a52 frame header */
165 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
166 if( p_a52_adec->p_fifo->b_die ) break;
168 /* check if frame is valid and get frame info */
169 p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
171 &p_a52_adec->sample_rate,
172 &p_a52_adec->bit_rate );
174 if( !p_a52_adec->frame_size )
176 intf_WarnMsg( 3, "a52: a52_syncinfo failed" );
180 if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
182 DecoderError( p_config->p_decoder_fifo );
189 /* If b_error is set, the decoder thread enters the error loop */
190 if( p_a52_adec->p_fifo->b_error )
192 DecoderError( p_a52_adec->p_fifo );
195 /* End of the liba52 decoder thread */
196 EndThread( p_a52_adec );
201 /*****************************************************************************
202 * InitThread: initialize data before entering main loop
203 *****************************************************************************/
204 static int InitThread( a52_adec_thread_t * p_a52_adec )
206 intf_WarnMsg( 3, "a52: InitThread" );
208 /* Initialize liba52 */
209 p_a52_adec->p_a52_state = a52_init( 0 );
210 if( p_a52_adec->p_a52_state == NULL )
212 intf_ErrMsg ( "a52 error: InitThread() unable to initialize "
217 p_a52_adec->b_dynrng = config_GetIntVariable( "a52-dynrng" );
219 /* Init the BitStream */
220 InitBitstream( &p_a52_adec->bit_stream,
222 BitstreamCallback, NULL );
227 /*****************************************************************************
228 * DecodeFrame: decodes an ATSC A/52 frame.
229 *****************************************************************************/
230 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
232 sample_t sample_level = 1;
236 if( ( p_a52_adec->p_aout_fifo != NULL ) &&
237 ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
239 /* Make sure the output thread leaves the NextFrame() function */
240 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
241 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
242 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
243 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
245 p_a52_adec->p_aout_fifo = NULL;
248 /* Creating the audio output fifo if not created yet */
249 if( p_a52_adec->p_aout_fifo == NULL )
251 p_a52_adec->p_aout_fifo = aout_CreateFifo( AOUT_FIFO_PCM,
252 p_a52_adec->i_channels,
253 p_a52_adec->sample_rate,
254 AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
257 if ( p_a52_adec->p_aout_fifo == NULL )
263 /* Set the Presentation Time Stamp */
264 CurrentPTS( &p_a52_adec->bit_stream,
265 &p_a52_adec->p_aout_fifo->date[
266 p_a52_adec->p_aout_fifo->i_end_frame],
269 if( !p_a52_adec->p_aout_fifo->date[
270 p_a52_adec->p_aout_fifo->i_end_frame] )
272 p_a52_adec->p_aout_fifo->date[
273 p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
278 p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
279 ( p_a52_adec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE *
280 p_a52_adec->i_channels * sizeof(s16) );
283 p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
285 /* Get the complete frame */
286 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
287 p_a52_adec->frame_size - 7 );
288 if( p_a52_adec->p_fifo->b_die ) return( -1 );
290 /* do the actual decoding now */
291 a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
292 &p_a52_adec->flags, &sample_level, 384 );
294 if( !p_a52_adec->b_dynrng )
295 a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
297 for( i = 0; i < 6; i++ )
299 if( a52_block( p_a52_adec->p_a52_state ) )
300 intf_WarnMsg( 5, "a52: a52_block failed for block %i", i );
302 float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
303 ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
307 vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
308 p_a52_adec->p_aout_fifo->i_end_frame =
309 (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
310 vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
311 vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
316 /*****************************************************************************
317 * EndThread : liba52 decoder thread destruction
318 *****************************************************************************/
319 static void EndThread (a52_adec_thread_t *p_a52_adec)
321 intf_WarnMsg ( 3, "a52: EndThread" );
323 /* If the audio output fifo was created, we destroy it */
324 if (p_a52_adec->p_aout_fifo != NULL)
326 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
328 /* Make sure the output thread leaves the NextFrame() function */
329 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
330 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
331 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
334 a52_free( p_a52_adec->p_a52_state );
339 /*****************************************************************************
340 * float2s16_2 : converts floats to ints using a trick based on the IEEE
341 * floating-point format
342 *****************************************************************************/
343 static inline int16_t convert (int32_t i)
347 else if (i < 0x43bf8000)
350 return i - 0x43c00000;
353 static void float2s16_2 (float * _f, int16_t * s16)
356 int32_t * f = (int32_t *) _f;
358 for (i = 0; i < 256; i++) {
359 s16[2*i] = convert (f[i]);
360 s16[2*i+1] = convert (f[i+256]);
364 /*****************************************************************************
365 * BitstreamCallback: Import parameters from the new data/PES packet
366 *****************************************************************************
367 * This function is called by input's NextDataPacket.
368 *****************************************************************************/
369 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
370 boolean_t b_new_pes )
374 /* Drop special AC3 header */
375 /* p_bit_stream->p_byte += 3; */