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 /** FIXME: Ugly but needed to access the counters */
43 #include "input/input_internal.h"
45 /*****************************************************************************
46 * aout_DecNew : create a decoder
47 *****************************************************************************/
48 static aout_input_t * DecNew( vlc_object_t * p_this, aout_instance_t * p_aout,
49 audio_sample_format_t *p_format,
50 audio_replay_gain_t *p_replay_gain )
52 aout_input_t * p_input;
53 input_thread_t * p_input_thread;
55 /* Sanitize audio format */
56 if( p_format->i_channels > 32 )
58 msg_Err( p_aout, "too many audio channels (%u)",
59 p_format->i_channels );
62 if( p_format->i_channels <= 0 )
64 msg_Err( p_aout, "no audio channels" );
68 if( p_format->i_rate > 192000 )
70 msg_Err( p_aout, "excessive audio sample frequency (%u)",
74 if( p_format->i_rate < 4000 )
76 msg_Err( p_aout, "too low audio sample frequency (%u)",
81 /* We can only be called by the decoder, so no need to lock
83 aout_lock_mixer( p_aout );
85 if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
87 msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
91 p_input = malloc(sizeof(aout_input_t));
92 if ( p_input == NULL )
94 memset( p_input, 0, sizeof(aout_input_t) );
96 vlc_mutex_init( &p_input->lock );
98 p_input->b_changed = 0;
101 aout_FormatPrepare( p_format );
103 memcpy( &p_input->input, p_format,
104 sizeof(audio_sample_format_t) );
106 p_input->replay_gain = *p_replay_gain;
108 aout_lock_input_fifos( p_aout );
109 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
110 p_aout->i_nb_inputs++;
112 if ( p_aout->mixer.b_error )
116 var_Destroy( p_aout, "audio-device" );
117 var_Destroy( p_aout, "audio-channels" );
119 /* Recreate the output using the new format. */
120 if ( aout_OutputNew( p_aout, p_format ) < 0 )
122 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
124 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
125 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
126 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
128 aout_unlock_input_fifos( p_aout );
129 aout_unlock_mixer( p_aout );
133 /* Create other input streams. */
134 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
136 aout_lock_input( p_aout, p_aout->pp_inputs[i] );
137 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
138 aout_InputNew( p_aout, p_aout->pp_inputs[i] );
139 aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
144 aout_MixerDelete( p_aout );
147 if ( aout_MixerNew( p_aout ) == -1 )
149 aout_OutputDelete( p_aout );
150 aout_unlock_input_fifos( p_aout );
154 aout_InputNew( p_aout, p_input );
155 aout_unlock_input_fifos( p_aout );
157 aout_unlock_mixer( p_aout );
158 p_input->i_desync = var_CreateGetInteger( p_this, "audio-desync" ) * 1000;
160 p_input_thread = (input_thread_t *)vlc_object_find( p_this,
161 VLC_OBJECT_INPUT, FIND_PARENT );
164 p_input->i_pts_delay = p_input_thread->i_pts_delay;
165 p_input->i_pts_delay += p_input->i_desync;
166 p_input->p_input_thread = p_input_thread;
167 vlc_object_release( p_input_thread );
171 p_input->i_pts_delay = DEFAULT_PTS_DELAY;
172 p_input->i_pts_delay += p_input->i_desync;
173 p_input->p_input_thread = NULL;
179 aout_unlock_mixer( p_aout );
183 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
184 aout_instance_t ** pp_aout,
185 audio_sample_format_t * p_format,
186 audio_replay_gain_t *p_replay_gain )
188 aout_instance_t *p_aout = *pp_aout;
189 if ( p_aout == NULL )
191 msg_Dbg( p_this, "no aout present, spawning one" );
192 p_aout = aout_New( p_this );
194 /* Everything failed, I'm a loser, I just wanna die */
198 vlc_object_attach( p_aout, p_this );
202 return DecNew( p_this, p_aout, p_format, p_replay_gain );
205 /*****************************************************************************
206 * aout_DecDelete : delete a decoder
207 *****************************************************************************/
208 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
212 /* This function can only be called by the decoder itself, so no need
213 * to lock p_input->lock. */
214 aout_lock_mixer( p_aout );
216 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
218 if ( p_aout->pp_inputs[i_input] == p_input )
224 if ( i_input == p_aout->i_nb_inputs )
226 msg_Err( p_aout, "cannot find an input to delete" );
227 aout_unlock_mixer( p_aout );
231 /* Remove the input from the list. */
232 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
233 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
234 p_aout->i_nb_inputs--;
236 aout_InputDelete( p_aout, p_input );
238 vlc_mutex_destroy( &p_input->lock );
241 if ( !p_aout->i_nb_inputs )
243 aout_OutputDelete( p_aout );
244 aout_MixerDelete( p_aout );
245 if ( var_Type( p_aout, "audio-device" ) != 0 )
247 var_Destroy( p_aout, "audio-device" );
249 if ( var_Type( p_aout, "audio-channels" ) != 0 )
251 var_Destroy( p_aout, "audio-channels" );
255 aout_unlock_mixer( p_aout );
265 /*****************************************************************************
266 * aout_DecNewBuffer : ask for a new empty buffer
267 *****************************************************************************/
268 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
269 size_t i_nb_samples )
271 aout_buffer_t * p_buffer;
274 aout_lock_input( NULL, p_input );
276 if ( p_input->b_error )
278 aout_unlock_input( NULL, p_input );
282 duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
284 /* This necessarily allocates in the heap. */
285 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
286 if( p_buffer != NULL )
287 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
288 / p_input->input.i_frame_length;
290 /* Suppose the decoder doesn't have more than one buffered buffer */
291 p_input->b_changed = 0;
293 aout_unlock_input( NULL, p_input );
295 if( p_buffer == NULL )
298 p_buffer->i_nb_samples = i_nb_samples;
299 p_buffer->start_date = p_buffer->end_date = 0;
303 /*****************************************************************************
304 * aout_DecDeleteBuffer : destroy an undecoded buffer
305 *****************************************************************************/
306 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
307 aout_buffer_t * p_buffer )
309 (void)p_aout; (void)p_input;
310 aout_BufferFree( p_buffer );
313 /*****************************************************************************
314 * aout_DecPlay : filter & mix the decoded buffer
315 *****************************************************************************/
316 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
317 aout_buffer_t * p_buffer, int i_input_rate )
319 if ( p_buffer->start_date == 0 )
321 msg_Warn( p_aout, "non-dated buffer received" );
322 aout_BufferFree( p_buffer );
327 /* This hack for #transcode{acodec=...}:display to work -- Courmisch */
328 if( i_input_rate == 0 )
329 i_input_rate = INPUT_RATE_DEFAULT;
331 if( i_input_rate > INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE ||
332 i_input_rate < INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE )
334 aout_BufferFree( p_buffer );
338 /* Apply the desynchronisation requested by the user */
339 p_buffer->start_date += p_input->i_desync;
340 p_buffer->end_date += p_input->i_desync;
342 if ( p_buffer->start_date > mdate() + p_input->i_pts_delay +
343 AOUT_MAX_ADVANCE_TIME )
345 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
346 p_buffer->start_date - mdate());
347 if( p_input->p_input_thread )
349 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
350 stats_UpdateInteger( p_aout,
351 p_input->p_input_thread->p->counters.p_lost_abuffers,
353 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
355 aout_BufferFree( p_buffer );
359 p_buffer->end_date = p_buffer->start_date
360 + (mtime_t)p_buffer->i_nb_samples * 1000000
361 / p_input->input.i_rate;
363 aout_lock_input( p_aout, p_input );
365 if ( p_input->b_error )
367 aout_unlock_input( p_aout, p_input );
368 aout_BufferFree( p_buffer );
372 if ( p_input->b_changed )
374 /* Maybe the allocation size has changed. Re-allocate a buffer. */
375 aout_buffer_t * p_new_buffer;
376 mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
377 / p_input->input.i_rate;
379 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
380 vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
381 p_buffer->i_nb_bytes );
382 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
383 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
384 p_new_buffer->start_date = p_buffer->start_date;
385 p_new_buffer->end_date = p_buffer->end_date;
386 aout_BufferFree( p_buffer );
387 p_buffer = p_new_buffer;
388 p_input->b_changed = 0;
391 /* If the buffer is too early, wait a while. */
392 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
394 int ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
396 aout_unlock_input( p_aout, p_input );
398 if ( ret == -1 ) return -1;
400 /* Run the mixer if it is able to run. */
401 aout_lock_mixer( p_aout );
402 aout_MixerRun( p_aout );
403 if( p_input->p_input_thread )
405 vlc_mutex_lock( &p_input->p_input_thread->p->counters.counters_lock);
406 stats_UpdateInteger( p_aout,
407 p_input->p_input_thread->p->counters.p_played_abuffers,
409 vlc_mutex_unlock( &p_input->p_input_thread->p->counters.counters_lock);
411 aout_unlock_mixer( p_aout );