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 *****************************************************************************/
33 #include <vlc_common.h>
36 #include <vlc_input.h>
38 #include "aout_internal.h"
42 * Creates an audio output
44 aout_input_t *aout_DecNew( aout_instance_t *p_aout,
45 audio_sample_format_t *p_format,
46 const audio_replay_gain_t *p_replay_gain,
47 const aout_request_vout_t *p_request_vout )
49 /* Sanitize audio format */
50 if( p_format->i_channels > 32 )
52 msg_Err( p_aout, "too many audio channels (%u)",
53 p_format->i_channels );
56 if( p_format->i_channels <= 0 )
58 msg_Err( p_aout, "no audio channels" );
61 if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
63 msg_Err( p_aout, "incompatible audio channels count with layout mask" );
67 if( p_format->i_rate > 192000 )
69 msg_Err( p_aout, "excessive audio sample frequency (%u)",
73 if( p_format->i_rate < 4000 )
75 msg_Err( p_aout, "too low audio sample frequency (%u)",
80 aout_input_t *p_input = calloc( 1, sizeof(aout_input_t));
84 vlc_mutex_init( &p_input->lock );
85 p_input->b_error = true;
86 p_input->b_paused = false;
87 p_input->i_pause_date = 0;
89 aout_FormatPrepare( p_format );
91 memcpy( &p_input->input, p_format,
92 sizeof(audio_sample_format_t) );
94 p_input->replay_gain = *p_replay_gain;
96 /* We can only be called by the decoder, so no need to lock
98 aout_lock_mixer( p_aout );
99 aout_lock_input_fifos( p_aout );
100 assert( p_aout->p_input == NULL );
101 p_aout->p_input = p_input;
103 var_Destroy( p_aout, "audio-device" );
104 var_Destroy( p_aout, "audio-channels" );
106 /* Recreate the output using the new format. */
107 if( aout_OutputNew( p_aout, p_format ) < 0 )
108 #warning Input without output and mixer = bad idea.
111 assert( p_aout->p_mixer == NULL );
112 if( aout_MixerNew( p_aout ) == -1 )
114 aout_OutputDelete( p_aout );
115 #warning Memory leak.
120 aout_InputNew( p_aout, p_input, p_request_vout );
122 aout_unlock_input_fifos( p_aout );
123 aout_unlock_mixer( p_aout );
127 /*****************************************************************************
128 * aout_DecDelete : delete a decoder
129 *****************************************************************************/
130 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
132 /* This function can only be called by the decoder itself, so no need
133 * to lock p_input->lock. */
134 aout_lock_mixer( p_aout );
136 if( p_input != p_aout->p_input )
138 msg_Err( p_aout, "cannot find an input to delete" );
139 aout_unlock_mixer( p_aout );
143 /* Remove the input. */
144 p_aout->p_input = NULL;
145 aout_InputDelete( p_aout, p_input );
147 aout_OutputDelete( p_aout );
148 aout_MixerDelete( p_aout );
149 var_Destroy( p_aout, "audio-device" );
150 var_Destroy( p_aout, "audio-channels" );
152 aout_unlock_mixer( p_aout );
154 vlc_mutex_destroy( &p_input->lock );
164 /*****************************************************************************
165 * aout_DecNewBuffer : ask for a new empty buffer
166 *****************************************************************************/
167 aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
168 size_t i_nb_samples )
173 aout_lock_input( NULL, p_input );
175 if ( p_input->b_error )
177 aout_unlock_input( NULL, p_input );
181 length = i_nb_samples * p_input->input.i_bytes_per_frame
182 / p_input->input.i_frame_length;
183 block = block_Alloc( length );
185 aout_unlock_input( NULL, p_input );
187 if( likely(block != NULL) )
189 block->i_nb_samples = i_nb_samples;
190 block->i_pts = block->i_length = 0;
195 /*****************************************************************************
196 * aout_DecDeleteBuffer : destroy an undecoded buffer
197 *****************************************************************************/
198 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
199 aout_buffer_t * p_buffer )
201 (void)p_aout; (void)p_input;
202 aout_BufferFree( p_buffer );
205 /*****************************************************************************
206 * aout_DecPlay : filter & mix the decoded buffer
207 *****************************************************************************/
208 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
209 aout_buffer_t * p_buffer, int i_input_rate )
211 assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
212 i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
214 assert( p_buffer->i_pts > 0 );
216 p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
217 / p_input->input.i_rate;
219 aout_lock_mixer( p_aout );
220 aout_lock_input( p_aout, p_input );
222 if( p_input->b_error )
224 aout_unlock_input( p_aout, p_input );
225 aout_unlock_mixer( p_aout );
227 aout_BufferFree( p_buffer );
231 aout_InputCheckAndRestart( p_aout, p_input );
232 aout_unlock_mixer( p_aout );
234 int i_ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
236 aout_unlock_input( p_aout, p_input );
241 /* Run the mixer if it is able to run. */
242 aout_lock_mixer( p_aout );
243 aout_MixerRun( p_aout, p_aout->mixer_multiplier );
244 aout_unlock_mixer( p_aout );
249 int aout_DecGetResetLost( aout_instance_t *p_aout, aout_input_t *p_input )
251 aout_lock_input( p_aout, p_input );
252 int i_value = p_input->i_buffer_lost;
253 p_input->i_buffer_lost = 0;
254 aout_unlock_input( p_aout, p_input );
259 void aout_DecChangePause( aout_instance_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
261 mtime_t i_duration = 0;
262 aout_lock_input( p_aout, p_input );
263 assert( !p_input->b_paused || !b_paused );
264 if( p_input->b_paused )
266 i_duration = i_date - p_input->i_pause_date;
268 p_input->b_paused = b_paused;
269 p_input->i_pause_date = i_date;
270 aout_unlock_input( p_aout, p_input );
272 if( i_duration != 0 )
274 aout_lock_mixer( p_aout );
275 for( aout_buffer_t *p = p_input->mixer.fifo.p_first; p != NULL; p = p->p_next )
277 p->i_pts += i_duration;
279 aout_unlock_mixer( p_aout );
283 void aout_DecFlush( aout_instance_t *p_aout, aout_input_t *p_input )
285 aout_lock_input_fifos( p_aout );
286 aout_FifoSet( &p_input->mixer.fifo, 0 );
287 aout_unlock_input_fifos( p_aout );