1 /*****************************************************************************
2 * dec.c : audio output API towards decoders
3 *****************************************************************************
4 * Copyright (C) 2002-2007 the VideoLAN team
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
38 #include <vlc_input.h>
40 #include "aout_internal.h"
42 /*****************************************************************************
43 * aout_DecNew : create a decoder
44 *****************************************************************************/
45 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
46 audio_sample_format_t *p_format,
47 audio_replay_gain_t *p_replay_gain )
49 aout_input_t * p_input;
51 /* Sanitize audio format */
52 if( p_format->i_channels > 32 )
54 msg_Err( p_aout, "too many audio channels (%u)",
55 p_format->i_channels );
58 if( p_format->i_channels <= 0 )
60 msg_Err( p_aout, "no audio channels" );
64 if( p_format->i_rate > 192000 )
66 msg_Err( p_aout, "excessive audio sample frequency (%u)",
70 if( p_format->i_rate < 4000 )
72 msg_Err( p_aout, "too low audio sample frequency (%u)",
77 /* We can only be called by the decoder, so no need to lock
79 aout_lock_mixer( p_aout );
81 if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
83 msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
87 p_input = malloc(sizeof(aout_input_t));
88 if ( p_input == NULL )
90 memset( p_input, 0, sizeof(aout_input_t) );
92 vlc_mutex_init( &p_input->lock );
94 p_input->b_changed = 0;
97 aout_FormatPrepare( p_format );
99 memcpy( &p_input->input, p_format,
100 sizeof(audio_sample_format_t) );
102 p_input->replay_gain = *p_replay_gain;
104 aout_lock_input_fifos( p_aout );
105 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
106 p_aout->i_nb_inputs++;
108 if ( p_aout->mixer.b_error )
112 var_Destroy( p_aout, "audio-device" );
113 var_Destroy( p_aout, "audio-channels" );
115 /* Recreate the output using the new format. */
116 if ( aout_OutputNew( p_aout, p_format ) < 0 )
118 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
120 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
121 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
122 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
124 aout_unlock_input_fifos( p_aout );
125 aout_unlock_mixer( p_aout );
129 /* Create other input streams. */
130 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
132 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
133 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
134 aout_InputNew( p_aout, p_aout->pp_inputs[i] );
135 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
140 aout_MixerDelete( p_aout );
143 if ( aout_MixerNew( p_aout ) == -1 )
145 aout_OutputDelete( p_aout );
146 aout_unlock_input_fifos( p_aout );
150 aout_InputNew( p_aout, p_input );
151 aout_unlock_input_fifos( p_aout );
153 aout_unlock_mixer( p_aout );
158 aout_unlock_mixer( p_aout );
162 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
163 aout_instance_t ** pp_aout,
164 audio_sample_format_t * p_format,
165 audio_replay_gain_t *p_replay_gain )
167 aout_instance_t *p_aout = *pp_aout;
168 if ( p_aout == NULL )
170 msg_Dbg( p_this, "no aout present, spawning one" );
171 p_aout = aout_New( p_this );
173 /* Everything failed, I'm a loser, I just wanna die */
177 vlc_object_attach( p_aout, p_this );
181 return DecNew( p_this, p_aout, p_format, p_replay_gain );
184 /*****************************************************************************
185 * aout_DecDelete : delete a decoder
186 *****************************************************************************/
187 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
191 /* This function can only be called by the decoder itself, so no need
192 * to lock p_input->lock. */
193 aout_lock_mixer( p_aout );
195 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
197 if ( p_aout->pp_inputs[i_input] == p_input )
203 if ( i_input == p_aout->i_nb_inputs )
205 msg_Err( p_aout, "cannot find an input to delete" );
206 aout_unlock_mixer( p_aout );
210 /* Remove the input from the list. */
211 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
212 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
213 p_aout->i_nb_inputs--;
215 aout_InputDelete( p_aout, p_input );
217 vlc_mutex_destroy( &p_input->lock );
220 if ( !p_aout->i_nb_inputs )
222 aout_OutputDelete( p_aout );
223 aout_MixerDelete( p_aout );
224 if ( var_Type( p_aout, "audio-device" ) != 0 )
226 var_Destroy( p_aout, "audio-device" );
228 if ( var_Type( p_aout, "audio-channels" ) != 0 )
230 var_Destroy( p_aout, "audio-channels" );
234 aout_unlock_mixer( p_aout );
244 /*****************************************************************************
245 * aout_DecNewBuffer : ask for a new empty buffer
246 *****************************************************************************/
247 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
248 size_t i_nb_samples )
250 aout_buffer_t * p_buffer;
253 aout_lock_input( NULL, p_input );
255 if ( p_input->b_error )
257 aout_unlock_input( NULL, p_input );
261 duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
263 /* This necessarily allocates in the heap. */
264 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
265 if( p_buffer != NULL )
266 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
267 / p_input->input.i_frame_length;
269 /* Suppose the decoder doesn't have more than one buffered buffer */
270 p_input->b_changed = 0;
272 aout_unlock_input( NULL, p_input );
274 if( p_buffer == NULL )
277 p_buffer->i_nb_samples = i_nb_samples;
278 p_buffer->start_date = p_buffer->end_date = 0;
282 /*****************************************************************************
283 * aout_DecDeleteBuffer : destroy an undecoded buffer
284 *****************************************************************************/
285 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
286 aout_buffer_t * p_buffer )
288 (void)p_aout; (void)p_input;
289 aout_BufferFree( p_buffer );
292 /*****************************************************************************
293 * aout_DecPlay : filter & mix the decoded buffer
294 *****************************************************************************/
295 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
296 aout_buffer_t * p_buffer, int i_input_rate )
298 assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
299 i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
301 assert( p_buffer->start_date > 0 );
303 p_buffer->end_date = p_buffer->start_date
304 + (mtime_t)p_buffer->i_nb_samples * 1000000
305 / p_input->input.i_rate;
307 aout_lock_input( p_aout, p_input );
309 if( p_input->b_error )
311 aout_unlock_input( p_aout, p_input );
312 aout_BufferFree( p_buffer );
316 if( p_input->b_changed )
318 /* Maybe the allocation size has changed. Re-allocate a buffer. */
319 aout_buffer_t * p_new_buffer;
320 mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
321 / p_input->input.i_rate;
323 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
324 vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
325 p_buffer->i_nb_bytes );
326 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
327 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
328 p_new_buffer->start_date = p_buffer->start_date;
329 p_new_buffer->end_date = p_buffer->end_date;
330 aout_BufferFree( p_buffer );
331 p_buffer = p_new_buffer;
332 p_input->b_changed = 0;
335 int i_ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
337 aout_unlock_input( p_aout, p_input );
342 /* Run the mixer if it is able to run. */
343 aout_lock_mixer( p_aout );
345 aout_MixerRun( p_aout );
347 aout_unlock_mixer( p_aout );
352 int aout_DecGetResetLost( aout_instance_t *p_aout, aout_input_t *p_input )
354 aout_lock_input( p_aout, p_input );
355 int i_value = p_input->i_buffer_lost;
356 p_input->i_buffer_lost = 0;
357 aout_unlock_input( p_aout, p_input );