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.1 2002/08/04 17:23:42 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() */
36 # include <stdint.h> /* int16_t .. */
38 # include <inttypes.h> /* int16_t .. */
41 #ifdef USE_A52DEC_TREE /* liba52 header file */
42 # include "include/a52.h"
44 # include "a52dec/a52.h"
49 #define A52DEC_FRAME_SIZE 1536
52 * Global lock for accessing liba52 functions.
53 * Currently, liba52 isn't thread-safe. So to prevent two threads from
54 * using liba52 at the same time, we have to set up a global lock.
55 * I know static variables aren't a good idea in multi-threaded programs,
56 * but believe me, this is the way to go.
59 static vlc_mutex_t a52_lock;
60 static vlc_bool_t b_liba52_initialized = 0;
63 /*****************************************************************************
65 *****************************************************************************/
66 static int OpenDecoder ( vlc_object_t * );
67 static int RunDecoder ( decoder_fifo_t * );
68 static int DecodeFrame ( a52_adec_thread_t * );
69 static int InitThread ( a52_adec_thread_t * );
70 static void EndThread ( a52_adec_thread_t * );
72 static void BitstreamCallback ( bit_stream_t *, vlc_bool_t );
73 static void float2s16_2 ( float *, int16_t * );
74 static inline int16_t convert ( int32_t );
76 /*****************************************************************************
78 *****************************************************************************/
79 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
80 #define DYNRNG_LONGTEXT N_( \
81 "Dynamic range compression makes the loud sounds softer, and the soft " \
82 "sounds louder, so you can more easily listen to the stream in a noisy " \
83 "environment without disturbing anyone. If you disable the dynamic range "\
84 "compression the playback will be more adapted to a movie theater or a " \
88 add_category_hint( N_("Miscellaneous"), NULL );
89 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT );
90 set_description( _("a52 ATSC A/52 aka AC-3 audio decoder module") );
91 set_capability( "decoder", 60 );
92 set_callbacks( OpenDecoder, NULL );
95 /*****************************************************************************
96 * OpenDecoder: probe the decoder and return score
97 *****************************************************************************
98 * Tries to launch a decoder and return score so that the interface is able
100 *****************************************************************************/
101 static int OpenDecoder( vlc_object_t *p_this )
103 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
105 if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
110 p_fifo->pf_run = RunDecoder;
114 /*****************************************************************************
115 * RunDecoder: this function is called just after the thread is created
116 *****************************************************************************/
117 static int RunDecoder( decoder_fifo_t *p_fifo )
119 a52_adec_thread_t *p_a52_adec;
121 /* Allocate the memory needed to store the thread's structure */
122 p_a52_adec = (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
123 if (p_a52_adec == NULL)
125 msg_Err( p_fifo, "out of memory" );
126 DecoderError( p_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_fifo = p_fifo;
139 if( InitThread( p_a52_adec ) )
141 msg_Err( p_a52_adec->p_fifo, "could not initialize thread" );
142 DecoderError( p_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 vlc_mutex_lock( &a52_lock );
165 p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
167 &p_a52_adec->sample_rate,
168 &p_a52_adec->bit_rate );
169 vlc_mutex_unlock( &a52_lock );
171 if( !p_a52_adec->frame_size )
173 msg_Warn( p_a52_adec->p_fifo, "a52_syncinfo failed" );
177 if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
179 DecoderError( p_fifo );
186 /* If b_error is set, the decoder thread enters the error loop */
187 if( p_a52_adec->p_fifo->b_error )
189 DecoderError( p_a52_adec->p_fifo );
192 /* End of the liba52 decoder thread */
193 EndThread( p_a52_adec );
198 /*****************************************************************************
199 * InitThread: initialize data before entering main loop
200 *****************************************************************************/
201 static int InitThread( a52_adec_thread_t * p_a52_adec )
203 /* Initialize the global lock */
204 vlc_mutex_lock( p_a52_adec->p_fifo->p_vlc->p_global_lock );
205 if ( !b_liba52_initialized )
207 vlc_mutex_init( p_a52_adec->p_fifo, &a52_lock );
208 b_liba52_initialized = 1;
210 vlc_mutex_unlock( p_a52_adec->p_fifo->p_vlc->p_global_lock );
212 /* Initialize liba52 */
213 vlc_mutex_lock( &a52_lock );
214 p_a52_adec->p_a52_state = a52_init( 0 );
215 vlc_mutex_unlock( &a52_lock );
216 if( p_a52_adec->p_a52_state == NULL )
218 msg_Err( p_a52_adec->p_fifo, "unable to initialize liba52" );
222 p_a52_adec->b_dynrng = config_GetInt( p_a52_adec->p_fifo, "a52-dynrng" );
224 /* Init the BitStream */
225 InitBitstream( &p_a52_adec->bit_stream,
227 BitstreamCallback, NULL );
232 /*****************************************************************************
233 * DecodeFrame: decodes an ATSC A/52 frame.
234 *****************************************************************************/
235 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
237 sample_t sample_level = 1;
241 if( ( p_a52_adec->p_aout_fifo != NULL ) &&
242 ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
244 /* Make sure the output thread leaves the NextFrame() function */
245 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
246 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
247 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
248 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
250 p_a52_adec->p_aout_fifo = NULL;
253 /* Creating the audio output fifo if not created yet */
254 if( p_a52_adec->p_aout_fifo == NULL )
256 p_a52_adec->p_aout_fifo = aout_CreateFifo( p_a52_adec->p_fifo,
257 AOUT_FIFO_PCM, p_a52_adec->i_channels,
258 p_a52_adec->sample_rate,
259 A52DEC_FRAME_SIZE * p_a52_adec->i_channels,
262 if ( p_a52_adec->p_aout_fifo == NULL )
268 /* Set the Presentation Time Stamp */
269 CurrentPTS( &p_a52_adec->bit_stream,
270 &p_a52_adec->p_aout_fifo->date[
271 p_a52_adec->p_aout_fifo->i_end_frame],
274 if( !p_a52_adec->p_aout_fifo->date[
275 p_a52_adec->p_aout_fifo->i_end_frame] )
277 p_a52_adec->p_aout_fifo->date[
278 p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
283 p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
284 ( p_a52_adec->p_aout_fifo->i_end_frame * A52DEC_FRAME_SIZE *
285 p_a52_adec->i_channels * sizeof(s16) );
288 p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
290 /* Get the complete frame */
291 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
292 p_a52_adec->frame_size - 7 );
293 if( p_a52_adec->p_fifo->b_die ) return( -1 );
295 /* do the actual decoding now */
296 vlc_mutex_lock( &a52_lock );
297 a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
298 &p_a52_adec->flags, &sample_level, 384 );
300 if( !p_a52_adec->b_dynrng )
301 a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
303 for( i = 0; i < 6; i++ )
305 if( a52_block( p_a52_adec->p_a52_state ) )
307 msg_Warn( p_a52_adec->p_fifo, "a52_block failed for block %i", i );
310 float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
311 ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
313 vlc_mutex_unlock( &a52_lock );
316 vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
317 p_a52_adec->p_aout_fifo->i_end_frame =
318 (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
319 vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
320 vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
325 /*****************************************************************************
326 * EndThread : liba52 decoder thread destruction
327 *****************************************************************************/
328 static void EndThread (a52_adec_thread_t *p_a52_adec)
330 /* If the audio output fifo was created, we destroy it */
331 if (p_a52_adec->p_aout_fifo != NULL)
333 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
335 /* Make sure the output thread leaves the NextFrame() function */
336 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
337 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
338 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
341 vlc_mutex_lock( &a52_lock );
342 a52_free( p_a52_adec->p_a52_state );
343 vlc_mutex_unlock( &a52_lock );
348 /*****************************************************************************
349 * float2s16_2 : converts floats to ints using a trick based on the IEEE
350 * floating-point format
351 *****************************************************************************/
352 static inline int16_t convert (int32_t i)
356 else if (i < 0x43bf8000)
359 return i - 0x43c00000;
362 static void float2s16_2 (float * _f, int16_t * s16)
365 int32_t * f = (int32_t *) _f;
367 for (i = 0; i < 256; i++) {
368 s16[2*i] = convert (f[i]);
369 s16[2*i+1] = convert (f[i+256]);
373 /*****************************************************************************
374 * BitstreamCallback: Import parameters from the new data/PES packet
375 *****************************************************************************
376 * This function is called by input's NextDataPacket.
377 *****************************************************************************/
378 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
379 vlc_bool_t b_new_pes )
383 /* Drop special A52 header */
384 /* p_bit_stream->p_byte += 3; */