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 $
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() */
31 #include <inttypes.h> /* int16_t .. */
33 #include <videolan/vlc.h>
35 #include "audio_output.h"
37 #include "stream_control.h"
38 #include "input_ext-dec.h"
40 #ifdef USE_A52DEC_TREE /* liba52 header file */
41 #include "include/a52.h"
43 #include "a52dec/a52.h"
48 #define AC3DEC_FRAME_SIZE 1536
50 /*****************************************************************************
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 * );
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 );
63 /*****************************************************************************
65 *****************************************************************************/
66 void _M( adec_getfunctions )( function_list_t * p_function_list )
68 p_function_list->functions.dec.pf_probe = decoder_Probe;
69 p_function_list->functions.dec.pf_run = decoder_Run;
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 " \
84 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
85 ADD_BOOL ( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT )
89 SET_DESCRIPTION( _("a52 ATSC A/52 aka AC-3 audio decoder module") )
90 ADD_CAPABILITY( DECODER, 40 )
95 _M( adec_getfunctions )( &p_module->p_functions->dec );
98 MODULE_DEACTIVATE_START
99 MODULE_DEACTIVATE_STOP
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
106 *****************************************************************************/
107 static int decoder_Probe( u8 *pi_type )
109 return ( *pi_type == AC3_AUDIO_ES ? 0 : -1 );
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 )
117 a52_adec_thread_t *p_a52_adec;
119 /* Allocate the memory needed to store the thread's structure */
121 (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
122 if (p_a52_adec == NULL)
124 intf_ErrMsg ( "a52 error: not enough memory "
125 "for decoder_Run() to allocate p_a52_adec" );
126 DecoderError( p_config->p_decoder_fifo );
131 p_a52_adec->i_channels = 2;
134 * Initialize the thread properties
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 ) )
141 intf_ErrMsg( "a52 error: could not initialize thread" );
142 DecoderError( p_config->p_decoder_fifo );
147 /* liba52 decoder thread's main loop */
148 while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
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))
156 RemoveBits( &p_a52_adec->bit_stream, 8 );
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;
163 /* check if frame is valid and get frame info */
164 p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
166 &p_a52_adec->sample_rate,
167 &p_a52_adec->bit_rate );
169 if( !p_a52_adec->frame_size )
171 intf_WarnMsg( 3, "a52: a52_syncinfo failed" );
175 if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
177 DecoderError( p_config->p_decoder_fifo );
184 /* If b_error is set, the decoder thread enters the error loop */
185 if( p_a52_adec->p_fifo->b_error )
187 DecoderError( p_a52_adec->p_fifo );
190 /* End of the liba52 decoder thread */
191 EndThread( p_a52_adec );
196 /*****************************************************************************
197 * InitThread: initialize data before entering main loop
198 *****************************************************************************/
199 static int InitThread( a52_adec_thread_t * p_a52_adec )
201 intf_WarnMsg( 3, "a52: InitThread" );
203 /* Initialize liba52 */
204 p_a52_adec->p_a52_state = a52_init( 0 );
205 if( p_a52_adec->p_a52_state == NULL )
207 intf_ErrMsg ( "a52 error: InitThread() unable to initialize "
212 p_a52_adec->b_dynrng = config_GetIntVariable( "a52-dynrng" );
214 /* Init the BitStream */
215 InitBitstream( &p_a52_adec->bit_stream,
217 BitstreamCallback, NULL );
222 /*****************************************************************************
223 * DecodeFrame: decodes an ATSC A/52 frame.
224 *****************************************************************************/
225 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
227 sample_t sample_level = 1;
231 if( ( p_a52_adec->p_aout_fifo != NULL ) &&
232 ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
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));
240 p_a52_adec->p_aout_fifo = NULL;
243 /* Creating the audio output fifo if not created yet */
244 if( p_a52_adec->p_aout_fifo == NULL )
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,
252 if ( p_a52_adec->p_aout_fifo == NULL )
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],
264 if( !p_a52_adec->p_aout_fifo->date[
265 p_a52_adec->p_aout_fifo->i_end_frame] )
267 p_a52_adec->p_aout_fifo->date[
268 p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
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) );
278 p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
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 );
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 );
289 if( !p_a52_adec->b_dynrng )
290 a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
292 for( i = 0; i < 6; i++ )
294 if( a52_block( p_a52_adec->p_a52_state ) )
295 intf_WarnMsg( 5, "a52: a52_block failed for block %i", i );
297 float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
298 ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
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);
311 /*****************************************************************************
312 * EndThread : liba52 decoder thread destruction
313 *****************************************************************************/
314 static void EndThread (a52_adec_thread_t *p_a52_adec)
316 intf_WarnMsg ( 3, "a52: EndThread" );
318 /* If the audio output fifo was created, we destroy it */
319 if (p_a52_adec->p_aout_fifo != NULL)
321 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
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));
329 a52_free( p_a52_adec->p_a52_state );
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)
342 else if (i < 0x43bf8000)
345 return i - 0x43c00000;
348 static void float2s16_2 (float * _f, int16_t * s16)
351 int32_t * f = (int32_t *) _f;
353 for (i = 0; i < 256; i++) {
354 s16[2*i] = convert (f[i]);
355 s16[2*i+1] = convert (f[i+256]);
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 )
369 /* Drop special AC3 header */
370 /* p_bit_stream->p_byte += 3; */