1 /*****************************************************************************
2 * decoder.c: MPEG audio decoder thread
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: decoder.c,v 1.11 2003/01/02 20:48:28 gbazin Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Michel Lespinasse <walken@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Cyril Deguet <asmax@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 *****************************************************************************/
30 #include <stdlib.h> /* malloc(), free() */
34 #include <vlc/decoder.h>
40 #define ADEC_FRAME_NB 1152
42 /*****************************************************************************
44 *****************************************************************************/
45 static int OpenDecoder ( vlc_object_t * );
46 static int RunDecoder ( decoder_fifo_t * );
48 static void EndThread ( adec_thread_t * );
49 static void DecodeThread ( adec_thread_t * );
51 /*****************************************************************************
53 *****************************************************************************/
55 set_description( _("MPEG I/II layer 1/2 audio decoder") );
56 set_capability( "decoder", 50 );
57 add_requirement( FPU );
58 add_shortcut( "builtin" );
59 set_callbacks( OpenDecoder, NULL );
62 /*****************************************************************************
63 * OpenDecoder: probe the decoder and return score
64 *****************************************************************************
65 * Tries to launch a decoder and return score so that the interface is able
67 *****************************************************************************/
68 static int OpenDecoder( vlc_object_t *p_this )
70 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
72 if( p_fifo->i_fourcc != VLC_FOURCC('m','p','g','a') )
77 p_fifo->pf_run = RunDecoder;
81 /*****************************************************************************
82 * RunDecoder: initialize, go inside main loop, destroy
83 *****************************************************************************/
84 static int RunDecoder( decoder_fifo_t *p_fifo )
86 adec_thread_t * p_dec;
88 /* Allocate the memory needed to store the thread's structure */
89 if ( (p_dec = (adec_thread_t *)malloc (sizeof(adec_thread_t))) == NULL )
91 msg_Err( p_fifo, "out of memory" );
92 DecoderError( p_fifo );
97 * Initialize the thread properties
99 p_dec->p_fifo = p_fifo;
102 * Initilize the banks
104 p_dec->bank_0.actual = p_dec->bank_0.v1;
105 p_dec->bank_0.pos = 0;
106 p_dec->bank_1.actual = p_dec->bank_1.v1;
107 p_dec->bank_1.pos = 0;
110 * Initialize bit stream
112 if( InitBitstream( &p_dec->bit_stream, p_dec->p_fifo, NULL, NULL )
115 msg_Err( p_fifo, "cannot initialize bitstream" );
116 DecoderError( p_fifo );
120 /* We do not create the audio output fifo now, but
121 it will be created when the first frame is received */
122 p_dec->p_aout = NULL;
123 p_dec->p_aout_input = NULL;
125 /* Audio decoder thread's main loop */
126 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
128 DecodeThread( p_dec );
131 /* If b_error is set, the audio decoder thread enters the error loop */
132 if( p_dec->p_fifo->b_error )
134 DecoderError( p_dec->p_fifo );
137 /* End of the audio decoder thread */
144 * Following functions are local to this module
147 /*****************************************************************************
148 * DecodeThread: decodes a mpeg frame
149 *****************************************************************************/
150 static void DecodeThread( adec_thread_t * p_dec )
153 aout_buffer_t * p_aout_buffer;
155 adec_sync_info_t sync_info;
157 /* Look for sync word - should be 0x0b77 */
158 RealignBits( &p_dec->bit_stream );
159 while( (ShowBits( &p_dec->bit_stream, 16 ) & 0xfff0) != 0xfff0 &&
160 (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error))
162 RemoveBits( &p_dec->bit_stream, 8 );
165 /* Set the Presentation Time Stamp */
166 NextPTS( &p_dec->bit_stream, &pts, NULL );
167 if ( pts != 0 && pts != aout_DateGet( &p_dec->end_date ) )
169 aout_DateSet( &p_dec->end_date, pts );
172 if( !adec_SyncFrame( p_dec, &sync_info ) )
174 /* Create the output fifo if it doesn't exist yet */
175 if( ( p_dec->p_aout_input == NULL ) ||
176 ( p_dec->output_format.i_physical_channels != (unsigned int)
177 ( sync_info.b_stereo ? AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
178 AOUT_CHAN_CENTER ) ) ||
179 ( p_dec->output_format.i_rate != sync_info.sample_rate ) )
181 if( p_dec->p_aout_input )
183 /* Delete old output */
184 msg_Warn( p_dec->p_fifo, "opening a new aout" );
185 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
188 /* Set output configuration */
189 p_dec->output_format.i_format = VLC_FOURCC('f','l','3','2');
190 p_dec->output_format.i_physical_channels =
191 p_dec->output_format.i_original_channels =
192 ( sync_info.b_stereo ? AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
194 p_dec->output_format.i_rate = sync_info.sample_rate;
195 aout_DateInit( &p_dec->end_date, sync_info.sample_rate );
196 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
198 &p_dec->output_format );
201 if( p_dec->p_aout_input == NULL )
203 msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
204 p_dec->p_fifo->b_error = 1;
208 if( !aout_DateGet( &p_dec->end_date ) )
210 /* We've just started the stream, wait for the first PTS. */
214 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout,
219 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
220 p_dec->p_fifo->b_error = 1;
223 p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
224 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
227 if( adec_DecodeFrame (p_dec, (float*)p_aout_buffer->p_buffer ) )
229 /* Ouch, failed decoding... We'll have to resync */
230 aout_DecDeleteBuffer( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
234 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
239 /*****************************************************************************
240 * EndThread : audio decoder thread destruction
241 *****************************************************************************
242 * This function is called when the thread ends after a sucessful
244 *****************************************************************************/
245 static void EndThread ( adec_thread_t *p_dec )
247 /* If the audio output fifo was created, we destroy it */
248 if( p_dec->p_aout_input )
250 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
254 CloseBitstream( &p_dec->bit_stream );
256 /* Destroy descriptor */