1 /*****************************************************************************
2 * dec.c : audio output API towards decoders
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: dec.c,v 1.1 2002/09/26 22:40:25 massiot Exp $
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include "audio_output.h"
37 #include "aout_internal.h"
43 /*****************************************************************************
44 * aout_DecNew : create a decoder
45 *****************************************************************************/
46 static aout_input_t * DecNew( aout_instance_t * p_aout,
47 audio_sample_format_t * p_format )
49 aout_input_t * p_input;
51 /* We can only be called by the decoder, so no need to lock
53 vlc_mutex_lock( &p_aout->mixer_lock );
55 if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
57 msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
61 p_input = malloc(sizeof(aout_input_t));
62 if ( p_input == NULL )
64 msg_Err( p_aout, "out of memory" );
68 vlc_mutex_init( p_aout, &p_input->lock );
70 p_input->b_changed = 0;
72 memcpy( &p_input->input, p_format,
73 sizeof(audio_sample_format_t) );
74 aout_FormatPrepare( &p_input->input );
76 p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
77 p_aout->i_nb_inputs++;
79 if ( p_aout->mixer.b_error )
83 /* Recreate the output using the new format. */
84 if ( aout_OutputNew( p_aout, p_format ) < 0 )
86 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
88 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
89 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
90 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
92 vlc_mutex_unlock( &p_aout->mixer_lock );
96 /* Create other input streams. */
97 for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
99 vlc_mutex_lock( &p_aout->pp_inputs[i]->lock );
100 aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
101 aout_InputNew( p_aout, p_aout->pp_inputs[i] );
102 vlc_mutex_unlock( &p_aout->pp_inputs[i]->lock );
107 aout_MixerDelete( p_aout );
110 if ( aout_MixerNew( p_aout ) == -1 )
112 aout_OutputDelete( p_aout );
113 vlc_mutex_unlock( &p_aout->mixer_lock );
117 aout_MixerNew( p_aout );
119 aout_InputNew( p_aout, p_input );
121 vlc_mutex_unlock( &p_aout->mixer_lock );
126 aout_input_t * __aout_DecNew( vlc_object_t * p_this,
127 aout_instance_t ** pp_aout,
128 audio_sample_format_t * p_format )
130 if ( *pp_aout == NULL )
132 /* Create an audio output if there is none. */
133 *pp_aout = vlc_object_find( p_this, VLC_OBJECT_AOUT, FIND_ANYWHERE );
135 if( *pp_aout == NULL )
137 msg_Dbg( p_this, "no aout present, spawning one" );
139 *pp_aout = aout_New( p_this );
140 /* Everything failed, I'm a loser, I just wanna die */
141 if( *pp_aout == NULL )
148 vlc_object_release( *pp_aout );
152 return DecNew( *pp_aout, p_format );
155 /*****************************************************************************
156 * aout_DecDelete : delete a decoder
157 *****************************************************************************/
158 int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
162 /* This function can only be called by the decoder itself, so no need
163 * to lock p_input->lock. */
164 vlc_mutex_lock( &p_aout->mixer_lock );
166 for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
168 if ( p_aout->pp_inputs[i_input] == p_input )
174 if ( i_input == p_aout->i_nb_inputs )
176 msg_Err( p_aout, "cannot find an input to delete" );
180 /* Remove the input from the list. */
181 memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
182 (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
183 p_aout->i_nb_inputs--;
185 aout_InputDelete( p_aout, p_input );
187 vlc_mutex_destroy( &p_input->lock );
190 if ( !p_aout->i_nb_inputs )
192 aout_OutputDelete( p_aout );
193 aout_MixerDelete( p_aout );
196 vlc_mutex_unlock( &p_aout->mixer_lock );
206 /*****************************************************************************
207 * aout_DecNewBuffer : ask for a new empty buffer
208 *****************************************************************************/
209 aout_buffer_t * aout_DecNewBuffer( aout_instance_t * p_aout,
210 aout_input_t * p_input,
211 size_t i_nb_samples )
213 aout_buffer_t * p_buffer;
216 vlc_mutex_lock( &p_input->lock );
218 if ( p_input->b_error )
220 vlc_mutex_unlock( &p_input->lock );
224 duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;
226 /* This necessarily allocates in the heap. */
227 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
228 p_buffer->i_nb_samples = i_nb_samples;
229 p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
230 / p_input->input.i_frame_length;
232 /* Suppose the decoder doesn't have more than one buffered buffer */
233 p_input->b_changed = 0;
235 vlc_mutex_unlock( &p_input->lock );
237 if ( p_buffer == NULL )
239 msg_Err( p_aout, "NULL buffer !" );
243 p_buffer->start_date = p_buffer->end_date = 0;
249 /*****************************************************************************
250 * aout_DecDeleteBuffer : destroy an undecoded buffer
251 *****************************************************************************/
252 void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
253 aout_buffer_t * p_buffer )
255 aout_BufferFree( p_buffer );
258 /*****************************************************************************
259 * aout_DecPlay : filter & mix the decoded buffer
260 *****************************************************************************/
261 int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
262 aout_buffer_t * p_buffer )
264 if ( p_buffer->start_date == 0 )
266 msg_Warn( p_aout, "non-dated buffer received" );
267 aout_BufferFree( p_buffer );
271 p_buffer->end_date = p_buffer->start_date
272 + (mtime_t)(p_buffer->i_nb_samples * 1000000)
273 / p_input->input.i_rate;
275 vlc_mutex_lock( &p_input->lock );
277 if ( p_input->b_error )
279 vlc_mutex_unlock( &p_input->lock );
280 aout_BufferFree( p_buffer );
284 if ( p_input->b_changed )
286 /* Maybe the allocation size has changed. Re-allocate a buffer. */
287 aout_buffer_t * p_new_buffer;
288 mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
289 / p_input->input.i_rate;
291 aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_new_buffer );
292 p_aout->p_vlc->pf_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
293 p_buffer->i_nb_bytes );
294 p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
295 p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
296 p_new_buffer->start_date = p_buffer->start_date;
297 p_new_buffer->end_date = p_buffer->end_date;
298 aout_BufferFree( p_buffer );
299 p_buffer = p_new_buffer;
300 p_input->b_changed = 0;
303 /* If the buffer is too early, wait a while. */
304 mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
306 if ( aout_InputPlay( p_aout, p_input, p_buffer ) == -1 )
308 vlc_mutex_unlock( &p_input->lock );
312 vlc_mutex_unlock( &p_input->lock );
314 /* Run the mixer if it is able to run. */
315 vlc_mutex_lock( &p_aout->mixer_lock );
316 aout_MixerRun( p_aout );
317 vlc_mutex_unlock( &p_aout->mixer_lock );