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.2 2002/08/07 21:36:56 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"
50 #define A52DEC_FRAME_SIZE 1536
53 /*****************************************************************************
55 *****************************************************************************/
56 static int OpenDecoder ( vlc_object_t * );
57 static int RunDecoder ( decoder_fifo_t * );
58 static int DecodeFrame ( a52_thread_t *, u8 * );
59 static int InitThread ( a52_thread_t *, decoder_fifo_t * );
60 static void EndThread ( a52_thread_t * );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
66 #define DYNRNG_LONGTEXT N_( \
67 "Dynamic range compression makes the loud sounds softer, and the soft " \
68 "sounds louder, so you can more easily listen to the stream in a noisy " \
69 "environment without disturbing anyone. If you disable the dynamic range "\
70 "compression the playback will be more adapted to a movie theater or a " \
74 add_category_hint( N_("Miscellaneous"), NULL );
75 add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT );
76 set_description( _("a52 ATSC A/52 aka AC-3 audio decoder module") );
77 set_capability( "decoder", 60 );
78 set_callbacks( OpenDecoder, NULL );
81 /*****************************************************************************
82 * OpenDecoder: probe the decoder and return score
83 *****************************************************************************
84 * Tries to launch a decoder and return score so that the interface is able
86 *****************************************************************************/
87 static int OpenDecoder( vlc_object_t *p_this )
89 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
91 if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
96 p_fifo->pf_run = RunDecoder;
100 /*****************************************************************************
101 * RunDecoder: this function is called just after the thread is created
102 *****************************************************************************/
103 static int RunDecoder( decoder_fifo_t *p_fifo )
107 /* Allocate the memory needed to store the thread's structure */
108 p_a52 = (a52_thread_t *)malloc( sizeof(a52_thread_t) );
111 msg_Err( p_fifo, "out of memory" );
112 DecoderError( p_fifo );
116 if( InitThread( p_a52, p_fifo ) )
118 msg_Err( p_a52->p_fifo, "could not initialize thread" );
119 DecoderError( p_fifo );
124 /* liba52 decoder thread's main loop */
125 while( !p_a52->p_fifo->b_die && !p_a52->p_fifo->b_error )
127 int i_frame_size, i_flags, i_rate, i_bit_rate;
129 /* Temporary buffer to store the raw frame to be decoded */
130 u8 p_frame_buffer[3840];
132 /* Look for sync word - should be 0x0b77 */
133 RealignBits(&p_a52->bit_stream);
134 while( (ShowBits( &p_a52->bit_stream, 16 ) ) != 0x0b77 &&
135 (!p_a52->p_fifo->b_die) && (!p_a52->p_fifo->b_error))
137 RemoveBits( &p_a52->bit_stream, 8 );
140 /* Get A/52 frame header */
141 GetChunk( &p_a52->bit_stream, p_frame_buffer, 7 );
142 if( p_a52->p_fifo->b_die ) break;
144 /* Check if frame is valid and get frame info */
145 i_frame_size = a52_syncinfo( p_frame_buffer, &i_flags, &i_rate,
150 msg_Warn( p_a52->p_fifo, "a52_syncinfo failed" );
154 if( (p_a52->p_aout_input != NULL) &&
155 ( (p_a52->output_format.i_rate != i_rate)
156 /* || (p_a52->output_format.i_channels != i_channels) */ ) )
158 /* Parameters changed - this should not happen. */
159 aout_InputDelete( p_a52->p_aout, p_a52->p_aout_input );
160 p_a52->p_aout_input = NULL;
163 /* Creating the audio input if not created yet. */
164 if( p_a52->p_aout_input == NULL )
166 p_a52->output_format.i_rate = i_rate;
167 /* p_a52->output_format.i_channels = i_channels; */
168 p_a52->p_aout_input = aout_InputNew( p_a52->p_fifo,
170 &p_a52->output_format );
172 if ( p_a52->p_aout_input == NULL )
174 p_a52->p_fifo->b_error = 1;
179 /* Set the Presentation Time Stamp */
180 CurrentPTS( &p_a52->bit_stream, &pts, NULL );
183 p_a52->last_date = pts;
186 /* Get the complete frame */
187 GetChunk( &p_a52->bit_stream, p_frame_buffer + 7,
189 if( p_a52->p_fifo->b_die ) break;
191 if( DecodeFrame( p_a52, p_frame_buffer ) )
193 p_a52->p_fifo->b_error = 1;
198 /* If b_error is set, the decoder thread enters the error loop */
199 if( p_a52->p_fifo->b_error )
201 DecoderError( p_a52->p_fifo );
204 /* End of the liba52 decoder thread */
210 /*****************************************************************************
211 * InitThread: initialize data before entering main loop
212 *****************************************************************************/
213 static int InitThread( a52_thread_t * p_a52, decoder_fifo_t * p_fifo )
215 /* Initialize the thread properties */
216 p_a52->p_aout = NULL;
217 p_a52->p_aout_input = NULL;
218 p_a52->p_fifo = p_fifo;
219 p_a52->output_format.i_format = AOUT_FMT_FLOAT32;
220 p_a52->output_format.i_channels = 2; /* FIXME ! */
221 p_a52->last_date = 0;
223 /* Initialize liba52 */
224 p_a52->p_a52_state = a52_init( 0 );
225 if( p_a52->p_a52_state == NULL )
227 msg_Err( p_a52->p_fifo, "unable to initialize liba52" );
231 p_a52->b_dynrng = config_GetInt( p_a52->p_fifo, "a52-dynrng" );
233 /* Init the BitStream */
234 InitBitstream( &p_a52->bit_stream, p_a52->p_fifo,
240 /*****************************************************************************
241 * Interleave: helper function to interleave channels
242 *****************************************************************************/
243 static void Interleave( float * p_out, float * p_in, int i_channels )
247 for ( j = 0; j < i_channels; j++ )
249 for ( i = 0; i < 256; i++ )
251 p_out[i * i_channels + j] = p_in[j * 256 + i];
256 /*****************************************************************************
257 * DecodeFrame: decode an ATSC A/52 frame.
258 *****************************************************************************/
259 static int DecodeFrame( a52_thread_t * p_a52, u8 * p_frame_buffer )
261 sample_t i_sample_level = 1;
262 aout_buffer_t * p_buffer;
264 int i_bytes_per_block = 256 * p_a52->output_format.i_channels
267 if( !p_a52->last_date )
269 /* We've just started the stream, wait for the first PTS. */
273 p_buffer = aout_BufferNew( p_a52->p_aout, p_a52->p_aout_input,
275 if ( p_buffer == NULL ) return -1;
276 p_buffer->start_date = p_a52->last_date;
277 p_a52->last_date += (mtime_t)(A52DEC_FRAME_SIZE * 1000000)
278 / p_a52->output_format.i_rate;
279 p_buffer->end_date = p_a52->last_date;
282 i_flags = A52_STEREO | A52_ADJUST_LEVEL;
284 /* Do the actual decoding now */
285 a52_frame( p_a52->p_a52_state, p_frame_buffer,
286 &i_flags, &i_sample_level, 0 );
288 if( !p_a52->b_dynrng )
290 a52_dynrng( p_a52->p_a52_state, NULL, NULL );
293 for ( i = 0; i < 6; i++ )
295 sample_t * p_samples;
297 if( a52_block( p_a52->p_a52_state ) )
299 msg_Warn( p_a52->p_fifo, "a52_block failed for block %i", i );
302 p_samples = a52_samples( p_a52->p_a52_state );
304 /* Interleave the *$£%ù samples */
305 Interleave( (float *)(p_buffer->p_buffer + i * i_bytes_per_block),
306 p_samples, p_a52->output_format.i_channels );
309 aout_BufferPlay( p_a52->p_aout, p_a52->p_aout_input, p_buffer );
314 /*****************************************************************************
315 * EndThread : liba52 decoder thread destruction
316 *****************************************************************************/
317 static void EndThread (a52_thread_t *p_a52)
319 if ( p_a52->p_aout_input != NULL )
321 aout_InputDelete( p_a52->p_aout, p_a52->p_aout_input );
324 a52_free( p_a52->p_a52_state );