]> git.sesse.net Git - vlc/blob - modules/codec/a52.c
* Miscellaneous S/PDIF fixes.
[vlc] / modules / codec / a52.c
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.7 2002/08/26 23:00:22 massiot Exp $
8  *
9  * Authors: Gildas Bazin <gbazin@netcourrier.com>
10  *          Christophe Massiot <massiot@via.ecp.fr>
11  *      
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.
16  * 
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <vlc/vlc.h>
31 #include <vlc/aout.h>
32 #include <vlc/decoder.h>
33
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <string.h>                                              /* strdup() */
36 #ifdef HAVE_STDINT_H
37 #   include <stdint.h>                                         /* int16_t .. */
38 #elif HAVE_INTTYPES_H
39 #   include <inttypes.h>                                       /* int16_t .. */
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #   include <unistd.h>
44 #endif
45
46 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
47 #   include "include/a52.h"
48 #else
49 #   include "a52dec/a52.h"
50 #endif
51
52 #define A52_FRAME_NB 1536 
53
54 /*****************************************************************************
55  * a52_thread_t : a52 decoder thread descriptor
56  *****************************************************************************/
57 typedef struct a52_thread_s
58 {
59     /*
60      * liba52 properties
61      */
62     a52_state_t *       p_a52_state;
63     vlc_bool_t          b_dynrng;
64
65     /* The bit stream structure handles the PES stream at the bit level */
66     bit_stream_t        bit_stream;
67
68     /*
69      * Input properties
70      */
71     decoder_fifo_t *    p_fifo;                /* stores the PES stream data */
72     data_packet_t *     p_data;
73
74     /*
75      * Output properties
76      */
77     aout_instance_t *   p_aout; /* opaque */
78     aout_input_t *      p_aout_input; /* opaque */
79     audio_sample_format_t output_format;
80     audio_date_t        end_date;
81 } a52_thread_t;
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int  OpenDecoder    ( vlc_object_t * );
87 static int  RunDecoder     ( decoder_fifo_t * );
88 static int  DecodeFrame    ( a52_thread_t *, byte_t * );
89 static int  InitThread     ( a52_thread_t *, decoder_fifo_t * );
90 static void EndThread      ( a52_thread_t * );
91
92 /*****************************************************************************
93  * Module descriptor
94  *****************************************************************************/
95 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
96 #define DYNRNG_LONGTEXT N_( \
97     "Dynamic range compression makes the loud sounds softer, and the soft " \
98     "sounds louder, so you can more easily listen to the stream in a noisy " \
99     "environment without disturbing anyone. If you disable the dynamic range "\
100     "compression the playback will be more adapted to a movie theater or a " \
101     "listening room.")
102
103 vlc_module_begin();
104     add_category_hint( N_("Miscellaneous"), NULL );
105     add_bool( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT );
106     set_description( _("a52 ATSC A/52 aka AC-3 audio decoder module") );
107     set_capability( "decoder", 60 );
108     set_callbacks( OpenDecoder, NULL );
109 vlc_module_end();
110
111 /*****************************************************************************
112  * OpenDecoder: probe the decoder and return score
113  *****************************************************************************
114  * Tries to launch a decoder and return score so that the interface is able
115  * to choose.
116  *****************************************************************************/
117 static int OpenDecoder( vlc_object_t *p_this )
118 {
119     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
120     
121     if( p_fifo->i_fourcc != VLC_FOURCC('a','5','2',' ') )
122     {   
123         return VLC_EGENERIC;
124     }
125
126     p_fifo->pf_run = RunDecoder;
127     return VLC_SUCCESS;
128 }
129
130 /*****************************************************************************
131  * RunDecoder: this function is called just after the thread is created
132  *****************************************************************************/
133 static int RunDecoder( decoder_fifo_t *p_fifo )
134 {
135     a52_thread_t *p_dec;
136
137     /* Allocate the memory needed to store the thread's structure */
138     p_dec = (a52_thread_t *)malloc( sizeof(a52_thread_t) );
139     if( p_dec == NULL )
140     {
141         msg_Err( p_fifo, "out of memory" );
142         DecoderError( p_fifo );
143         return -1;
144     }
145
146     if( InitThread( p_dec, p_fifo ) )
147     {
148         msg_Err( p_dec->p_fifo, "could not initialize thread" );
149         DecoderError( p_fifo );
150         free( p_dec );
151         return -1;
152     }
153
154     /* liba52 decoder thread's main loop */
155     while( !p_dec->p_fifo->b_die && !p_dec->p_fifo->b_error )
156     {
157         int i_frame_size, i_flags, i_rate, i_bit_rate;
158         mtime_t pts;
159         /* Temporary buffer to store the raw frame to be decoded */
160         byte_t p_frame_buffer[3840];
161
162         /* Look for sync word - should be 0x0b77 */
163         RealignBits( &p_dec->bit_stream );
164         while( (ShowBits( &p_dec->bit_stream, 16 ) ) != 0x0b77 && 
165                (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error))
166         {
167             RemoveBits( &p_dec->bit_stream, 8 );
168         }
169
170         /* Set the Presentation Time Stamp */
171         NextPTS( &p_dec->bit_stream, &pts, NULL );
172         if ( pts != 0 && pts != aout_DateGet( &p_dec->end_date ) )
173         {
174             aout_DateSet( &p_dec->end_date, pts );
175         }
176
177         /* Get A/52 frame header */
178         GetChunk( &p_dec->bit_stream, p_frame_buffer, 7 );
179         if( p_dec->p_fifo->b_die ) break;
180
181         /* Check if frame is valid and get frame info */
182         i_frame_size = a52_syncinfo( p_frame_buffer, &i_flags, &i_rate,
183                                      &i_bit_rate );
184
185         if( !i_frame_size )
186         {
187             msg_Warn( p_dec->p_fifo, "a52_syncinfo failed" );
188             continue;
189         }
190
191         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         {
195             /* Parameters changed - this should not happen. */
196             aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
197             p_dec->p_aout_input = NULL;
198         }
199
200         /* Creating the audio input if not created yet. */
201         if( p_dec->p_aout_input == NULL )
202         {
203             p_dec->output_format.i_rate = i_rate;
204             /* p_dec->output_format.i_channels = i_channels; */
205             aout_DateInit( &p_dec->end_date, i_rate );
206             p_dec->p_aout_input = aout_InputNew( p_dec->p_fifo,
207                                                  &p_dec->p_aout,
208                                                  &p_dec->output_format );
209
210             if ( p_dec->p_aout_input == NULL )
211             {
212                 p_dec->p_fifo->b_error = 1;
213                 break;
214             }
215         }
216
217         /* Get the complete frame */
218         GetChunk( &p_dec->bit_stream, p_frame_buffer + 7,
219                   i_frame_size - 7 );
220         if( p_dec->p_fifo->b_die ) break;
221
222         if( DecodeFrame( p_dec, p_frame_buffer ) )
223         {
224             p_dec->p_fifo->b_error = 1;
225             break;
226         }
227     }
228
229     /* If b_error is set, the decoder thread enters the error loop */
230     if( p_dec->p_fifo->b_error )
231     {
232         DecoderError( p_dec->p_fifo );
233     }
234
235     /* End of the a52 decoder thread */
236     EndThread( p_dec );
237
238     return 0;
239 }
240
241 /*****************************************************************************
242  * InitThread: initialize data before entering main loop
243  *****************************************************************************/
244 static int InitThread( a52_thread_t * p_dec, decoder_fifo_t * p_fifo )
245 {
246     /* Initialize the thread properties */
247     p_dec->p_aout = NULL;
248     p_dec->p_aout_input = NULL;
249     p_dec->p_fifo = p_fifo;
250     p_dec->output_format.i_format = AOUT_FMT_FLOAT32;
251     p_dec->output_format.i_channels = 2; /* FIXME ! */
252
253     /* Initialize liba52 */
254     p_dec->p_a52_state = a52_init( 0 );
255     if( p_dec->p_a52_state == NULL )
256     {
257         msg_Err( p_dec->p_fifo, "unable to initialize liba52" );
258         return -1;
259     }
260
261     p_dec->b_dynrng = config_GetInt( p_dec->p_fifo, "a52-dynrng" );
262
263     /* Init the Bitstream */
264     InitBitstream( &p_dec->bit_stream, p_dec->p_fifo,
265                    NULL, NULL );
266
267     return 0;
268 }
269
270 /*****************************************************************************
271  * Interleave: helper function to interleave channels
272  *****************************************************************************/
273 static void Interleave( float * p_out, const float * p_in, int i_channels )
274 {
275     int i, j;
276
277     for ( j = 0; j < i_channels; j++ )
278     {
279         for ( i = 0; i < 256; i++ )
280         {
281             p_out[i * i_channels + j] = p_in[j * 256 + i];
282         }
283     }
284 }
285
286 /*****************************************************************************
287  * DecodeFrame: decode an ATSC A/52 frame.
288  *****************************************************************************/
289 static int DecodeFrame( a52_thread_t * p_dec, byte_t * p_frame_buffer )
290 {
291     sample_t        i_sample_level = 1;
292     aout_buffer_t * p_buffer;
293     int             i, i_flags;
294     int             i_bytes_per_block = 256 * p_dec->output_format.i_channels
295                       * sizeof(float);
296
297     if( !aout_DateGet( &p_dec->end_date ) )
298     {
299         /* We've just started the stream, wait for the first PTS. */
300         return 0;
301     }
302
303     p_buffer = aout_BufferNew( p_dec->p_aout, p_dec->p_aout_input,
304                                A52_FRAME_NB );
305     if ( p_buffer == NULL ) return -1;
306     p_buffer->start_date = aout_DateGet( &p_dec->end_date );
307     p_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
308                                              A52_FRAME_NB );
309
310     /* FIXME */
311     i_flags = A52_STEREO | A52_ADJUST_LEVEL;
312
313     /* Do the actual decoding now */
314     a52_frame( p_dec->p_a52_state, p_frame_buffer,
315                &i_flags, &i_sample_level, 0 );
316
317     if( !p_dec->b_dynrng )
318     {
319         a52_dynrng( p_dec->p_a52_state, NULL, NULL );
320     }
321
322     for ( i = 0; i < 6; i++ )
323     {
324         sample_t * p_samples;
325
326         if( a52_block( p_dec->p_a52_state ) )
327         {
328             msg_Warn( p_dec->p_fifo, "a52_block failed for block %i", i );
329         }
330
331         p_samples = a52_samples( p_dec->p_a52_state );
332
333         /* Interleave the *$£%ù samples */
334         Interleave( (float *)(p_buffer->p_buffer + i * i_bytes_per_block),
335                     p_samples, p_dec->output_format.i_channels );
336     }
337
338     aout_BufferPlay( p_dec->p_aout, p_dec->p_aout_input, p_buffer );
339
340     return 0;
341 }
342
343 /*****************************************************************************
344  * EndThread : liba52 decoder thread destruction
345  *****************************************************************************/
346 static void EndThread( a52_thread_t * p_dec )
347 {
348     if ( p_dec->p_aout_input != NULL )
349     {
350         aout_InputDelete( p_dec->p_aout, p_dec->p_aout_input );
351     }
352
353     a52_free( p_dec->p_a52_state );
354     free( p_dec );
355 }
356