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.3 2002/02/15 13:32:52 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 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <string.h> /* strdup() */
33 #include <videolan/vlc.h>
35 #include "audio_output.h"
37 #include "stream_control.h"
38 #include "input_ext-dec.h"
42 #include <a52dec/a52.h> /* liba52 header file */
45 #define AC3DEC_FRAME_SIZE 1536
47 /*****************************************************************************
49 *****************************************************************************/
50 static int decoder_Probe ( u8 * );
51 static int decoder_Run ( decoder_config_t * );
52 static int DecodeFrame ( a52_adec_thread_t * );
53 static int InitThread ( a52_adec_thread_t * );
54 static void EndThread ( a52_adec_thread_t * );
56 static void BitstreamCallback ( bit_stream_t *, boolean_t );
57 static void float2s16_2 ( float *, int16_t * );
58 static __inline__ int16_t convert ( int32_t );
60 /*****************************************************************************
62 *****************************************************************************/
63 void _M( adec_getfunctions )( function_list_t * p_function_list )
65 p_function_list->functions.dec.pf_probe = decoder_Probe;
66 p_function_list->functions.dec.pf_run = decoder_Run;
69 /*****************************************************************************
70 * Build configuration tree.
71 *****************************************************************************/
76 SET_DESCRIPTION( "a52 ATSC A/52 aka AC-3 audio decoder plugin" )
77 ADD_CAPABILITY( DECODER, 40 )
82 _M( adec_getfunctions )( &p_module->p_functions->dec );
85 MODULE_DEACTIVATE_START
86 MODULE_DEACTIVATE_STOP
88 /*****************************************************************************
89 * decoder_Probe: probe the decoder and return score
90 *****************************************************************************
91 * Tries to launch a decoder and return score so that the interface is able
93 *****************************************************************************/
94 static int decoder_Probe( u8 *pi_type )
96 return ( *pi_type == AC3_AUDIO_ES ? 0 : -1 );
99 /*****************************************************************************
100 * decoder_Run: this function is called just after the thread is created
101 *****************************************************************************/
102 static int decoder_Run ( decoder_config_t *p_config )
104 a52_adec_thread_t *p_a52_adec;
106 /* Allocate the memory needed to store the thread's structure */
108 (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
109 if (p_a52_adec == NULL)
111 intf_ErrMsg ( "a52 error: not enough memory "
112 "for decoder_Run() to allocate p_a52_adec" );
113 DecoderError( p_config->p_decoder_fifo );
118 p_a52_adec->i_channels = 2;
121 * Initialize the thread properties
123 p_a52_adec->p_aout_fifo = NULL;
124 p_a52_adec->p_config = p_config;
125 p_a52_adec->p_fifo = p_a52_adec->p_config->p_decoder_fifo;
126 if( InitThread( p_a52_adec ) )
128 intf_ErrMsg( "a52 error: could not initialize thread" );
129 DecoderError( p_config->p_decoder_fifo );
134 /* liba52 decoder thread's main loop */
135 while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
138 /* look for sync word - should be 0x0b77 */
139 RealignBits(&p_a52_adec->bit_stream);
140 while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 &&
141 (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
143 RemoveBits( &p_a52_adec->bit_stream, 8 );
146 /* get a52 frame header */
147 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
149 /* check if frame is valid and get frame info */
150 p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
152 &p_a52_adec->sample_rate,
153 &p_a52_adec->bit_rate );
155 if( !p_a52_adec->frame_size )
157 intf_WarnMsg( 3, "a52: a52_syncinfo failed" );
161 if( DecodeFrame( p_a52_adec ) )
163 DecoderError( p_config->p_decoder_fifo );
170 /* If b_error is set, the decoder thread enters the error loop */
171 if( p_a52_adec->p_fifo->b_error )
173 DecoderError( p_a52_adec->p_fifo );
176 /* End of the liba52 decoder thread */
177 EndThread( p_a52_adec );
182 /*****************************************************************************
183 * InitThread: initialize data before entering main loop
184 *****************************************************************************/
185 static int InitThread( a52_adec_thread_t * p_a52_adec )
187 intf_WarnMsg( 3, "a52: InitThread" );
189 /* Initialize liba52 */
190 p_a52_adec->p_decoded_samples = a52_init( 0 );
191 if( p_a52_adec->p_decoded_samples == NULL )
193 intf_ErrMsg ( "a52 error: InitThread() unable to initialize "
198 /* Init the BitStream */
199 InitBitstream( &p_a52_adec->bit_stream,
201 BitstreamCallback, NULL );
206 /*****************************************************************************
207 * DecodeFrame: decodes an ATSC A/52 frame.
208 *****************************************************************************/
209 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
211 sample_t sample_level = 1;
215 if( ( p_a52_adec->p_aout_fifo != NULL ) &&
216 ( p_a52_adec->p_aout_fifo->l_rate != p_a52_adec->sample_rate ) )
218 /* Make sure the output thread leaves the NextFrame() function */
219 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
220 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
221 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
222 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
224 p_a52_adec->p_aout_fifo = NULL;
227 /* Creating the audio output fifo if not created yet */
228 if (p_a52_adec->p_aout_fifo == NULL )
230 p_a52_adec->p_aout_fifo = aout_CreateFifo( AOUT_ADEC_STEREO_FIFO,
231 p_a52_adec->i_channels,
232 p_a52_adec->sample_rate, 0,
233 AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
236 if ( p_a52_adec->p_aout_fifo == NULL )
242 /* Set the Presentation Time Stamp */
243 CurrentPTS( &p_a52_adec->bit_stream,
244 &p_a52_adec->p_aout_fifo->date[
245 p_a52_adec->p_aout_fifo->l_end_frame],
248 if( !p_a52_adec->p_aout_fifo->date[
249 p_a52_adec->p_aout_fifo->l_end_frame] )
251 p_a52_adec->p_aout_fifo->date[
252 p_a52_adec->p_aout_fifo->l_end_frame] = LAST_MDATE;
257 p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
258 ( p_a52_adec->p_aout_fifo->l_end_frame * AC3DEC_FRAME_SIZE *
259 p_a52_adec->i_channels * sizeof(s16) );
262 p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
264 /* Get the complete frame */
265 GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
266 p_a52_adec->frame_size - 7 );
268 /* do the actual decoding now */
269 a52_frame( &p_a52_adec->a52_state, p_a52_adec->p_frame_buffer,
270 &p_a52_adec->flags, &sample_level, 384 );
272 for( i = 0; i < 6; i++ )
274 if( a52_block(&p_a52_adec->a52_state, p_a52_adec->p_decoded_samples) )
275 intf_WarnMsg( 5, "a52: a52_block failed for block %i", i );
277 float2s16_2( p_a52_adec->p_decoded_samples,
278 ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
282 vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
283 p_a52_adec->p_aout_fifo->l_end_frame =
284 (p_a52_adec->p_aout_fifo->l_end_frame + 1) & AOUT_FIFO_SIZE;
285 vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
286 vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
291 /*****************************************************************************
292 * EndThread : liba52 decoder thread destruction
293 *****************************************************************************/
294 static void EndThread (a52_adec_thread_t *p_a52_adec)
296 intf_WarnMsg ( 3, "a52: EndThread" );
298 /* If the audio output fifo was created, we destroy it */
299 if (p_a52_adec->p_aout_fifo != NULL)
301 aout_DestroyFifo (p_a52_adec->p_aout_fifo);
303 /* Make sure the output thread leaves the NextFrame() function */
304 vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
305 vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
306 vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
313 /*****************************************************************************
314 * float2s16_2 : converts floats to ints using a trick based on the IEEE
315 * floating-point format
316 *****************************************************************************/
317 static __inline__ int16_t convert (int32_t i)
321 else if (i < 0x43bf8000)
324 return i - 0x43c00000;
327 static void float2s16_2 (float * _f, int16_t * s16)
330 int32_t * f = (int32_t *) _f;
332 for (i = 0; i < 256; i++) {
333 s16[2*i] = convert (f[i]);
334 s16[2*i+1] = convert (f[i+256]);
338 /*****************************************************************************
339 * BitstreamCallback: Import parameters from the new data/PES packet
340 *****************************************************************************
341 * This function is called by input's NextDataPacket.
342 *****************************************************************************/
343 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
344 boolean_t b_new_pes )
348 /* Drop special AC3 header */
349 p_bit_stream->p_byte += 3;