]> git.sesse.net Git - vlc/blob - plugins/a52/a52.c
* ALL: got rid of p_object->p_this which is now useless.
[vlc] / plugins / a52 / 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 VideoLAN
7  * $Id: a52.c,v 1.18 2002/06/01 18:04:48 sam Exp $
8  *
9  * Authors: Gildas Bazin <gbazin@netcourrier.com>
10  *      
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.
15  * 
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <vlc/vlc.h>
30 #include <vlc/aout.h>
31 #include <vlc/decoder.h>
32
33 #include <stdlib.h>                                      /* malloc(), free() */
34 #include <string.h>                                              /* strdup() */
35 #ifdef HAVE_INTTYPES_H
36 #   include <inttypes.h>                                       /* int16_t .. */
37 #endif
38
39 #ifdef USE_A52DEC_TREE                                 /* liba52 header file */
40 #   include "include/a52.h"
41 #else
42 #   include "a52dec/a52.h"
43 #endif
44
45 #include "a52.h"
46
47 #define AC3DEC_FRAME_SIZE 1536 
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  decoder_Probe  ( u8 * );
53 static int  decoder_Run    ( decoder_fifo_t * );
54 static int  DecodeFrame    ( a52_adec_thread_t * );
55 static int  InitThread     ( a52_adec_thread_t * );
56 static void EndThread      ( a52_adec_thread_t * );
57
58 static void               BitstreamCallback ( bit_stream_t *, vlc_bool_t );
59 static void               float2s16_2       ( float *, int16_t * );
60 static inline int16_t     convert   ( int32_t );
61
62 /*****************************************************************************
63  * Capabilities
64  *****************************************************************************/
65 void _M( adec_getfunctions )( function_list_t * p_function_list )
66 {
67     p_function_list->functions.dec.pf_probe = decoder_Probe;
68     p_function_list->functions.dec.pf_run   = decoder_Run;
69 }
70
71 /*****************************************************************************
72  * Build configuration structure.
73  *****************************************************************************/
74 #define DYNRNG_TEXT N_("A/52 dynamic range compression")
75 #define DYNRNG_LONGTEXT N_( \
76     "Dynamic range compression makes the loud sounds softer, and the soft " \
77     "sounds louder, so you can more easily listen to the stream in a noisy " \
78     "environment without disturbing anyone. If you disable the dynamic range "\
79     "compression the playback will be more adapted to a movie theater or a " \
80     "listening room.")
81
82 MODULE_CONFIG_START
83 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
84 ADD_BOOL    ( "a52-dynrng", 1, NULL, DYNRNG_TEXT, DYNRNG_LONGTEXT )
85 MODULE_CONFIG_STOP
86
87 MODULE_INIT_START
88     SET_DESCRIPTION( _("a52 ATSC A/52 aka AC-3 audio decoder module") )
89     ADD_CAPABILITY( DECODER, 40 )
90 MODULE_INIT_STOP
91
92 MODULE_ACTIVATE_START
93     _M( adec_getfunctions )( &p_module->p_functions->dec );
94 MODULE_ACTIVATE_STOP
95
96 MODULE_DEACTIVATE_START
97 MODULE_DEACTIVATE_STOP
98
99 /*****************************************************************************
100  * decoder_Probe: probe the decoder and return score
101  *****************************************************************************
102  * Tries to launch a decoder and return score so that the interface is able
103  * to choose.
104  *****************************************************************************/
105 static int decoder_Probe( u8 *pi_type )
106 {
107     return ( *pi_type == AC3_AUDIO_ES ? 0 : -1 );
108 }
109
110 /*****************************************************************************
111  * decoder_Run: this function is called just after the thread is created
112  *****************************************************************************/
113 static int decoder_Run ( decoder_fifo_t *p_fifo )
114 {
115     a52_adec_thread_t *p_a52_adec;
116
117     /* Allocate the memory needed to store the thread's structure */
118     p_a52_adec = (a52_adec_thread_t *)malloc( sizeof(a52_adec_thread_t) );
119     if (p_a52_adec == NULL)
120     {
121         msg_Err( p_fifo, "out of memory" );
122         DecoderError( p_fifo );
123         return( -1 );
124     }
125
126     /* FIXME */
127     p_a52_adec->i_channels = 2;
128
129     /*
130      * Initialize the thread properties
131      */
132     p_a52_adec->p_aout_fifo = NULL;
133     p_a52_adec->p_fifo = p_fifo;
134
135     if( InitThread( p_a52_adec ) )
136     {
137         msg_Err( p_a52_adec->p_fifo, "could not initialize thread" );
138         DecoderError( p_fifo );
139         free( p_a52_adec );
140         return( -1 );
141     }
142
143     /* liba52 decoder thread's main loop */
144     while( !p_a52_adec->p_fifo->b_die && !p_a52_adec->p_fifo->b_error )
145     {
146
147         /* look for sync word - should be 0x0b77 */
148         RealignBits(&p_a52_adec->bit_stream);
149         while( (ShowBits( &p_a52_adec->bit_stream, 16 ) ) != 0x0b77 && 
150                (!p_a52_adec->p_fifo->b_die) && (!p_a52_adec->p_fifo->b_error))
151         {
152             RemoveBits( &p_a52_adec->bit_stream, 8 );
153         }
154
155         /* get a52 frame header */
156         GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer, 7 );
157         if( p_a52_adec->p_fifo->b_die ) break;
158
159         /* check if frame is valid and get frame info */
160         p_a52_adec->frame_size = a52_syncinfo( p_a52_adec->p_frame_buffer,
161                                                &p_a52_adec->flags,
162                                                &p_a52_adec->sample_rate,
163                                                &p_a52_adec->bit_rate );
164
165         if( !p_a52_adec->frame_size )
166         {
167             msg_Warn( p_a52_adec->p_fifo, "a52_syncinfo failed" );
168             continue;
169         }
170
171         if( DecodeFrame( p_a52_adec ) && !p_a52_adec->p_fifo->b_die )
172         {
173             DecoderError( p_fifo );
174             free( p_a52_adec );
175             return( -1 );
176         }
177
178     }
179
180     /* If b_error is set, the decoder thread enters the error loop */
181     if( p_a52_adec->p_fifo->b_error )
182     {
183         DecoderError( p_a52_adec->p_fifo );
184     }
185
186     /* End of the liba52 decoder thread */
187     EndThread( p_a52_adec );
188
189     return( 0 );
190 }
191
192 /*****************************************************************************
193  * InitThread: initialize data before entering main loop
194  *****************************************************************************/
195 static int InitThread( a52_adec_thread_t * p_a52_adec )
196 {
197     /* Initialize liba52 */
198     p_a52_adec->p_a52_state = a52_init( 0 );
199     if( p_a52_adec->p_a52_state == NULL )
200     {
201         msg_Err( p_a52_adec->p_fifo, "unable to initialize liba52" );
202         return -1;
203     }
204
205     p_a52_adec->b_dynrng = config_GetInt( p_a52_adec->p_fifo, "a52-dynrng" );
206
207     /* Init the BitStream */
208     InitBitstream( &p_a52_adec->bit_stream,
209                    p_a52_adec->p_fifo,
210                    BitstreamCallback, NULL );
211
212     return( 0 );
213 }
214
215 /*****************************************************************************
216  * DecodeFrame: decodes an ATSC A/52 frame.
217  *****************************************************************************/
218 static int DecodeFrame( a52_adec_thread_t * p_a52_adec )
219 {
220     sample_t sample_level = 1;
221     byte_t   *p_buffer;
222     int i;
223
224     if( ( p_a52_adec->p_aout_fifo != NULL ) &&
225         ( p_a52_adec->p_aout_fifo->i_rate != p_a52_adec->sample_rate ) )
226     {
227         /* Make sure the output thread leaves the NextFrame() function */
228         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
229         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
230         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
231         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
232
233         p_a52_adec->p_aout_fifo = NULL;
234     }
235
236     /* Creating the audio output fifo if not created yet */
237     if( p_a52_adec->p_aout_fifo == NULL )
238     {
239         p_a52_adec->p_aout_fifo = aout_CreateFifo( p_a52_adec->p_fifo,
240                                     AOUT_FIFO_PCM, p_a52_adec->i_channels,
241                                     p_a52_adec->sample_rate,
242                                     AC3DEC_FRAME_SIZE * p_a52_adec->i_channels,
243                                     NULL );
244
245         if ( p_a52_adec->p_aout_fifo == NULL )
246         { 
247             return( -1 );
248         }
249     }
250
251     /* Set the Presentation Time Stamp */
252     CurrentPTS( &p_a52_adec->bit_stream,
253                 &p_a52_adec->p_aout_fifo->date[
254                     p_a52_adec->p_aout_fifo->i_end_frame],
255                 NULL );
256
257     if( !p_a52_adec->p_aout_fifo->date[
258             p_a52_adec->p_aout_fifo->i_end_frame] )
259     {
260         p_a52_adec->p_aout_fifo->date[
261             p_a52_adec->p_aout_fifo->i_end_frame] = LAST_MDATE;
262     }
263
264
265
266     p_buffer = ((byte_t *)p_a52_adec->p_aout_fifo->buffer) +
267         ( p_a52_adec->p_aout_fifo->i_end_frame * AC3DEC_FRAME_SIZE *
268           p_a52_adec->i_channels * sizeof(s16) );
269
270     /* FIXME */
271     p_a52_adec->flags = A52_STEREO | A52_ADJUST_LEVEL;
272
273     /* Get the complete frame */
274     GetChunk( &p_a52_adec->bit_stream, p_a52_adec->p_frame_buffer + 7,
275               p_a52_adec->frame_size - 7 );
276     if( p_a52_adec->p_fifo->b_die ) return( -1 );
277
278     /* do the actual decoding now */
279     a52_frame( p_a52_adec->p_a52_state, p_a52_adec->p_frame_buffer,
280                &p_a52_adec->flags, &sample_level, 384 );
281
282     if( !p_a52_adec->b_dynrng )
283         a52_dynrng( p_a52_adec->p_a52_state, NULL, NULL );
284
285     for( i = 0; i < 6; i++ )
286     {
287         if( a52_block( p_a52_adec->p_a52_state ) )
288         {
289             msg_Warn( p_a52_adec->p_fifo, "a52_block failed for block %i", i );
290         }
291
292         float2s16_2( a52_samples( p_a52_adec->p_a52_state ),
293                      ((int16_t *)p_buffer) + i * 256 * p_a52_adec->i_channels );
294     }
295
296
297     vlc_mutex_lock( &p_a52_adec->p_aout_fifo->data_lock );
298     p_a52_adec->p_aout_fifo->i_end_frame = 
299       (p_a52_adec->p_aout_fifo->i_end_frame + 1) & AOUT_FIFO_SIZE;
300     vlc_cond_signal (&p_a52_adec->p_aout_fifo->data_wait);
301     vlc_mutex_unlock (&p_a52_adec->p_aout_fifo->data_lock);
302
303     return 0;
304 }
305
306 /*****************************************************************************
307  * EndThread : liba52 decoder thread destruction
308  *****************************************************************************/
309 static void EndThread (a52_adec_thread_t *p_a52_adec)
310 {
311     /* If the audio output fifo was created, we destroy it */
312     if (p_a52_adec->p_aout_fifo != NULL)
313     {
314         aout_DestroyFifo (p_a52_adec->p_aout_fifo);
315
316         /* Make sure the output thread leaves the NextFrame() function */
317         vlc_mutex_lock (&(p_a52_adec->p_aout_fifo->data_lock));
318         vlc_cond_signal (&(p_a52_adec->p_aout_fifo->data_wait));
319         vlc_mutex_unlock (&(p_a52_adec->p_aout_fifo->data_lock));
320     }
321
322     a52_free( p_a52_adec->p_a52_state );
323     free( p_a52_adec );
324
325 }
326
327 /*****************************************************************************
328  * float2s16_2 : converts floats to ints using a trick based on the IEEE
329  *               floating-point format
330  *****************************************************************************/
331 static inline int16_t convert (int32_t i)
332 {
333     if (i > 0x43c07fff)
334         return 32767;
335     else if (i < 0x43bf8000)
336         return -32768;
337     else
338         return i - 0x43c00000;
339 }
340
341 static void float2s16_2 (float * _f, int16_t * s16)
342 {
343     int i;
344     int32_t * f = (int32_t *) _f;
345
346     for (i = 0; i < 256; i++) {
347       s16[2*i] = convert (f[i]);
348         s16[2*i+1] = convert (f[i+256]);
349     }
350 }
351
352 /*****************************************************************************
353  * BitstreamCallback: Import parameters from the new data/PES packet
354  *****************************************************************************
355  * This function is called by input's NextDataPacket.
356  *****************************************************************************/
357 static void BitstreamCallback ( bit_stream_t * p_bit_stream,
358                                 vlc_bool_t b_new_pes )
359 {
360     if( b_new_pes )
361     {
362         /* Drop special AC3 header */
363 /*        p_bit_stream->p_byte += 3; */
364     }
365 }