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.18 2002/06/01 18:04:48 sam 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 *****************************************************************************/
31 #include <vlc/decoder.h>
33 #include <stdlib.h> /* malloc(), free() */
34 #include <string.h> /* strdup() */
35 #ifdef HAVE_INTTYPES_H
36 # include <inttypes.h> /* int16_t .. */
39 #ifdef USE_A52DEC_TREE /* liba52 header file */
40 # include "include/a52.h"
42 # include "a52dec/a52.h"
47 #define AC3DEC_FRAME_SIZE 1536
49 /*****************************************************************************
51 *****************************************************************************/
52 static int decoder_Probe ( u8 * );
53 static int decoder_Run ( decoder_fifo_t * );
54 static int DecodeFrame ( a52_adec_thread_t * );
55 static int InitThread ( a52_adec_thread_t * );
56 static void EndThread ( a52_adec_thread_t * );
58 static void BitstreamCallback ( bit_stream_t *, vlc_bool_t );
59 static void float2s16_2 ( float *, int16_t * );
60 static inline int16_t convert ( int32_t );
62 /*****************************************************************************
64 *****************************************************************************/
65 void _M( adec_getfunctions )( function_list_t * p_function_list )
67 p_function_list->functions.dec.pf_probe = decoder_Probe;
68 p_function_list->functions.dec.pf_run = decoder_Run;
71 /*****************************************************************************
72 * Build configuration structure.
73 *****************************************************************************/
74 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
75 #define DYNRNG_LONGTEXT N_( \
76 "Dynamic range compression makes the loud sounds softer, and the soft " \
77 "sounds louder, so you can more easily listen to the stream in a noisy " \
78 "environment without disturbing anyone. If you disable the dynamic range "\
79 "compression the playback will be more adapted to a movie theater or a " \
83 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
84 ADD_BOOL ( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT )
88 SET_DESCRIPTION( _("a52 ATSC A/52 aka AC-3 audio decoder module") )
89 ADD_CAPABILITY( DECODER, 40 )
93 _M( adec_getfunctions )( &p_module->p_functions->dec );
96 MODULE_DEACTIVATE_START
97 MODULE_DEACTIVATE_STOP
99 /*****************************************************************************
100 * decoder_Probe: probe the decoder and return score
101 *****************************************************************************
102 * Tries to launch a decoder and return score so that the interface is able
104 *****************************************************************************/
105 static int decoder_Probe( u8 *pi_type )
107 return ( *pi_type == AC3_AUDIO_ES ? 0 : -1 );
110 /*****************************************************************************
111 * decoder_Run: this function is called just after the thread is created
112 *****************************************************************************/
113 static int decoder_Run ( decoder_fifo_t *p_fifo )
115 a52_adec_thread_t *p_a52_adec;
117 /* Allocate the memory needed to store the thread's structure */
118 p_a52_adec = (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
119 if (p_a52_adec == NULL)
121 msg_Err( p_fifo, "out of memory" );
122 DecoderError( p_fifo );
127 p_a52_adec->i_channels = 2;
130 * Initialize the thread properties
132 p_a52_adec->p_aout_fifo = NULL;
133 p_a52_adec->p_fifo = p_fifo;
135 if( InitThread( p_a52_adec ) )
137 msg_Err( p_a52_adec->p_fifo, "could not initialize thread" );
138 DecoderError( p_fifo );
143 /* liba52 decoder thread's main loop */
144 while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
147 /* look for sync word - should be 0x0b77 */
148 RealignBits(&p_a52_adec->bit_stream);
149 while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 &&
150 (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
152 RemoveBits( &p_a52_adec->bit_stream, 8 );
155 /* get a52 frame header */
156 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
157 if( p_a52_adec->p_fifo->b_die ) break;
159 /* check if frame is valid and get frame info */
160 p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
162 &p_a52_adec->sample_rate,
163 &p_a52_adec->bit_rate );
165 if( !p_a52_adec->frame_size )
167 msg_Warn( p_a52_adec->p_fifo, "a52_syncinfo failed" );
171 if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
173 DecoderError( p_fifo );
180 /* If b_error is set, the decoder thread enters the error loop */
181 if( p_a52_adec->p_fifo->b_error )
183 DecoderError( p_a52_adec->p_fifo );
186 /* End of the liba52 decoder thread */
187 EndThread( p_a52_adec );
192 /*****************************************************************************
193 * InitThread: initialize data before entering main loop
194 *****************************************************************************/
195 static int InitThread( a52_adec_thread_t * p_a52_adec )
197 /* Initialize liba52 */
198 p_a52_adec->p_a52_state = a52_init( 0 );
199 if( p_a52_adec->p_a52_state == NULL )
201 msg_Err( p_a52_adec->p_fifo, "unable to initialize liba52" );
205 p_a52_adec->b_dynrng = config_GetInt( p_a52_adec->p_fifo, "a52-dynrng" );
207 /* Init the BitStream */
208 InitBitstream( &p_a52_adec->bit_stream,
210 BitstreamCallback, NULL );
215 /*****************************************************************************
216 * DecodeFrame: decodes an ATSC A/52 frame.
217 *****************************************************************************/
218 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
220 sample_t sample_level = 1;
224 if( ( p_a52_adec->p_aout_fifo != NULL ) &&
225 ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
227 /* Make sure the output thread leaves the NextFrame() function */
228 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
229 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
230 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
231 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
233 p_a52_adec->p_aout_fifo = NULL;
236 /* Creating the audio output fifo if not created yet */
237 if( p_a52_adec->p_aout_fifo == NULL )
239 p_a52_adec->p_aout_fifo = aout_CreateFifo( p_a52_adec->p_fifo,
240 AOUT_FIFO_PCM, p_a52_adec->i_channels,
241 p_a52_adec->sample_rate,
242 AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
245 if ( p_a52_adec->p_aout_fifo == NULL )
251 /* Set the Presentation Time Stamp */
252 CurrentPTS( &p_a52_adec->bit_stream,
253 &p_a52_adec->p_aout_fifo->date[
254 p_a52_adec->p_aout_fifo->i_end_frame],
257 if( !p_a52_adec->p_aout_fifo->date[
258 p_a52_adec->p_aout_fifo->i_end_frame] )
260 p_a52_adec->p_aout_fifo->date[
261 p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
266 p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
267 ( p_a52_adec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE *
268 p_a52_adec->i_channels * sizeof(s16) );
271 p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
273 /* Get the complete frame */
274 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
275 p_a52_adec->frame_size - 7 );
276 if( p_a52_adec->p_fifo->b_die ) return( -1 );
278 /* do the actual decoding now */
279 a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
280 &p_a52_adec->flags, &sample_level, 384 );
282 if( !p_a52_adec->b_dynrng )
283 a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
285 for( i = 0; i < 6; i++ )
287 if( a52_block( p_a52_adec->p_a52_state ) )
289 msg_Warn( p_a52_adec->p_fifo, "a52_block failed for block %i", i );
292 float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
293 ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
297 vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
298 p_a52_adec->p_aout_fifo->i_end_frame =
299 (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
300 vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
301 vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
306 /*****************************************************************************
307 * EndThread : liba52 decoder thread destruction
308 *****************************************************************************/
309 static void EndThread (a52_adec_thread_t *p_a52_adec)
311 /* If the audio output fifo was created, we destroy it */
312 if (p_a52_adec->p_aout_fifo != NULL)
314 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
316 /* Make sure the output thread leaves the NextFrame() function */
317 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
318 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
319 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
322 a52_free( p_a52_adec->p_a52_state );
327 /*****************************************************************************
328 * float2s16_2 : converts floats to ints using a trick based on the IEEE
329 * floating-point format
330 *****************************************************************************/
331 static inline int16_t convert (int32_t i)
335 else if (i < 0x43bf8000)
338 return i - 0x43c00000;
341 static void float2s16_2 (float * _f, int16_t * s16)
344 int32_t * f = (int32_t *) _f;
346 for (i = 0; i < 256; i++) {
347 s16[2*i] = convert (f[i]);
348 s16[2*i+1] = convert (f[i+256]);
352 /*****************************************************************************
353 * BitstreamCallback: Import parameters from the new data/PES packet
354 *****************************************************************************
355 * This function is called by input's NextDataPacket.
356 *****************************************************************************/
357 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
358 vlc_bool_t b_new_pes )
362 /* Drop special AC3 header */
363 /* p_bit_stream->p_byte += 3; */