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, 2002 VideoLAN
7 * $Id: a52.c,v 1.1 2002/08/11 01:27:01 massiot Exp $
9 * Authors: Gildas Bazin <gbazin@netcourrier.com>
10 * Christophe Massiot <massiot@via.ecp.fr>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
32 #include <vlc/decoder.h>
34 #include <stdlib.h> /* malloc(), free() */
35 #include <string.h> /* strdup() */
37 # include <stdint.h> /* int16_t .. */
39 # include <inttypes.h> /* int16_t .. */
42 #ifdef USE_A52DEC_TREE /* liba52 header file */
43 # include "include/a52.h"
45 # include "a52dec/a52.h"
48 #define A52_FRAME_SIZE 1536
50 /*****************************************************************************
51 * a52_thread_t : a52 decoder thread descriptor
52 *****************************************************************************/
53 typedef struct a52_thread_s
58 a52_state_t * p_a52_state;
61 /* The bit stream structure handles the PES stream at the bit level */
62 bit_stream_t bit_stream;
67 decoder_fifo_t * p_fifo; /* stores the PES stream data */
68 data_packet_t * p_data;
73 aout_instance_t * p_aout; /* opaque */
74 aout_input_t * p_aout_input; /* opaque */
75 audio_sample_format_t output_format;
79 /*****************************************************************************
81 *****************************************************************************/
82 static int OpenDecoder ( vlc_object_t * );
83 static int RunDecoder ( decoder_fifo_t * );
84 static int DecodeFrame ( a52_thread_t *, const byte_t * );
85 static int InitThread ( a52_thread_t *, decoder_fifo_t * );
86 static void EndThread ( a52_thread_t * );
88 /*****************************************************************************
90 *****************************************************************************/
91 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
92 #define DYNRNG_LONGTEXT N_( \
93 "Dynamic range compression makes the loud sounds softer, and the soft " \
94 "sounds louder, so you can more easily listen to the stream in a noisy " \
95 "environment without disturbing anyone. If you disable the dynamic range "\
96 "compression the playback will be more adapted to a movie theater or a " \
100 add_category_hint( N_("Miscellaneous"), NULL );
101 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT );
102 set_description( _("a52 ATSC A/52 aka AC-3 audio decoder module") );
103 set_capability( "decoder", 60 );
104 set_callbacks( OpenDecoder, NULL );
107 /*****************************************************************************
108 * OpenDecoder: probe the decoder and return score
109 *****************************************************************************
110 * Tries to launch a decoder and return score so that the interface is able
112 *****************************************************************************/
113 static int OpenDecoder( vlc_object_t *p_this )
115 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
117 if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
122 p_fifo->pf_run = RunDecoder;
126 /*****************************************************************************
127 * RunDecoder: this function is called just after the thread is created
128 *****************************************************************************/
129 static int RunDecoder( decoder_fifo_t *p_fifo )
133 /* Allocate the memory needed to store the thread's structure */
134 p_dec = (a52_thread_t *)malloc( sizeof(a52_thread_t) );
137 msg_Err( p_fifo, "out of memory" );
138 DecoderError( p_fifo );
142 if( InitThread( p_dec, p_fifo ) )
144 msg_Err( p_dec->p_fifo, "could not initialize thread" );
145 DecoderError( p_fifo );
150 /* liba52 decoder thread's main loop */
151 while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
153 int i_frame_size, i_flags, i_rate, i_bit_rate;
155 /* Temporary buffer to store the raw frame to be decoded */
156 byte_t p_frame_buffer[3840];
158 /* Look for sync word - should be 0x0b77 */
159 RealignBits( &p_dec->bit_stream );
160 while( (ShowBits( &p_dec->bit_stream, 16 ) ) != 0x0b77 &&
161 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error))
163 RemoveBits( &p_dec->bit_stream, 8 );
166 /* Get A/52 frame header */
167 GetChunk( &p_dec->bit_stream, p_frame_buffer, 7 );
168 if( p_dec->p_fifo->b_die ) break;
170 /* Check if frame is valid and get frame info */
171 i_frame_size = a52_syncinfo( p_frame_buffer, &i_flags, &i_rate,
176 msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
180 if( (p_dec->p_aout_input != NULL) &&
181 ( (p_dec->output_format.i_rate != i_rate)
182 /* || (p_dec->output_format.i_channels != i_channels) */ ) )
184 /* Parameters changed - this should not happen. */
185 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
186 p_dec->p_aout_input = NULL;
189 /* Creating the audio input if not created yet. */
190 if( p_dec->p_aout_input == NULL )
192 p_dec->output_format.i_rate = i_rate;
193 /* p_dec->output_format.i_channels = i_channels; */
194 p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
196 &p_dec->output_format );
198 if ( p_dec->p_aout_input == NULL )
200 p_dec->p_fifo->b_error = 1;
205 /* Set the Presentation Time Stamp */
206 CurrentPTS( &p_dec->bit_stream, &pts, NULL );
209 p_dec->last_date = pts;
212 /* Get the complete frame */
213 GetChunk( &p_dec->bit_stream, p_frame_buffer + 7,
215 if( p_dec->p_fifo->b_die ) break;
217 if( DecodeFrame( p_dec, p_frame_buffer ) )
219 p_dec->p_fifo->b_error = 1;
224 /* If b_error is set, the decoder thread enters the error loop */
225 if( p_dec->p_fifo->b_error )
227 DecoderError( p_dec->p_fifo );
230 /* End of the a52 decoder thread */
236 /*****************************************************************************
237 * InitThread: initialize data before entering main loop
238 *****************************************************************************/
239 static int InitThread( a52_thread_t * p_dec, decoder_fifo_t * p_fifo )
241 /* Initialize the thread properties */
242 p_dec->p_aout = NULL;
243 p_dec->p_aout_input = NULL;
244 p_dec->p_fifo = p_fifo;
245 p_dec->output_format.i_format = AOUT_FMT_FLOAT32;
246 p_dec->output_format.i_channels = 2; /* FIXME ! */
247 p_dec->last_date = 0;
249 /* Initialize liba52 */
250 p_dec->p_a52_state = a52_init( 0 );
251 if( p_dec->p_a52_state == NULL )
253 msg_Err( p_dec->p_fifo, "unable to initialize liba52" );
257 p_dec->b_dynrng = config_GetInt( p_dec->p_fifo, "a52-dynrng" );
259 /* Init the Bitstream */
260 InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
266 /*****************************************************************************
267 * Interleave: helper function to interleave channels
268 *****************************************************************************/
269 static void Interleave( float * p_out, const float * p_in, int i_channels )
273 for ( j = 0; j < i_channels; j++ )
275 for ( i = 0; i < 256; i++ )
277 p_out[i * i_channels + j] = p_in[j * 256 + i];
282 /*****************************************************************************
283 * DecodeFrame: decode an ATSC A/52 frame.
284 *****************************************************************************/
285 static int DecodeFrame( a52_thread_t * p_dec, const byte_t * p_frame_buffer )
287 sample_t i_sample_level = 1;
288 aout_buffer_t * p_buffer;
290 int i_bytes_per_block = 256 * p_dec->output_format.i_channels
293 if( !p_dec->last_date )
295 /* We've just started the stream, wait for the first PTS. */
299 p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input,
301 if ( p_buffer == NULL ) return -1;
302 p_buffer->start_date = p_dec->last_date;
303 p_dec->last_date += (mtime_t)(A52_FRAME_SIZE * 1000000)
304 / p_dec->output_format.i_rate;
305 p_buffer->end_date = p_dec->last_date;
308 i_flags = A52_STEREO | A52_ADJUST_LEVEL;
310 /* Do the actual decoding now */
311 a52_frame( p_dec->p_a52_state, p_frame_buffer,
312 &i_flags, &i_sample_level, 0 );
314 if( !p_dec->b_dynrng )
316 a52_dynrng( p_dec->p_a52_state, NULL, NULL );
319 for ( i = 0; i < 6; i++ )
321 sample_t * p_samples;
323 if( a52_block( p_dec->p_a52_state ) )
325 msg_Warn( p_dec->p_fifo, "a52_block failed for block %i", i );
328 p_samples = a52_samples( p_dec->p_a52_state );
330 /* Interleave the *$£%ù samples */
331 Interleave( (float *)(p_buffer->p_buffer + i * i_bytes_per_block),
332 p_samples, p_dec->output_format.i_channels );
335 aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
340 /*****************************************************************************
341 * EndThread : liba52 decoder thread destruction
342 *****************************************************************************/
343 static void EndThread( a52_thread_t * p_dec )
345 if ( p_dec->p_aout_input != NULL )
347 aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
350 a52_free( p_dec->p_a52_state );