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 int aout_DecNew( audio_output_t *p_aout,
45 const 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 p_input->b_error = true;
87 p_input->replay_gain = *p_replay_gain;
89 /* We can only be called by the decoder, so no need to lock
91 aout_owner_t *owner = aout_owner(p_aout);
93 assert (owner->input == NULL);
95 var_Destroy( p_aout, "audio-device" );
96 var_Destroy( p_aout, "audio-channels" );
98 /* Recreate the output using the new format. */
99 owner->input_format = *p_format;
100 if( aout_OutputNew( p_aout, p_format ) < 0 )
103 assert (owner->volume.mixer == NULL);
104 owner->volume.mixer = aout_MixerNew (p_aout, owner->mixer_format.i_format);
106 date_Init (&owner->sync.date, owner->mixer_format.i_rate, 1);
107 date_Set (&owner->sync.date, VLC_TS_INVALID);
109 owner->input = p_input;
110 aout_InputNew( p_aout, p_format, p_input, p_request_vout );
111 aout_unlock( p_aout );
114 aout_unlock( p_aout );
119 /*****************************************************************************
120 * aout_DecDelete : delete a decoder
121 *****************************************************************************/
122 void aout_DecDelete( audio_output_t * p_aout )
124 aout_owner_t *owner = aout_owner (p_aout);
126 struct audio_mixer *mixer;
129 /* Remove the input. */
130 input = owner->input;
131 aout_InputDelete (p_aout, input);
134 aout_OutputDelete( p_aout );
135 mixer = owner->volume.mixer;
136 owner->volume.mixer = NULL;
137 var_Destroy( p_aout, "audio-device" );
138 var_Destroy( p_aout, "audio-channels" );
140 aout_unlock( p_aout );
142 aout_MixerDelete (mixer);
146 static void aout_CheckRestart (audio_output_t *aout)
148 aout_owner_t *owner = aout_owner (aout);
149 aout_input_t *input = owner->input;
151 aout_assert_locked (aout);
153 if (likely(!owner->need_restart))
155 owner->need_restart = false;
157 /* Reinitializes the output */
158 aout_InputDelete (aout, owner->input);
159 aout_MixerDelete (owner->volume.mixer);
160 owner->volume.mixer = NULL;
161 aout_OutputDelete (aout);
163 if (aout_OutputNew (aout, &owner->input_format))
165 input->b_error = true;
166 return; /* we are officially screwed */
169 owner->volume.mixer = aout_MixerNew (aout, owner->mixer_format.i_format);
171 if (aout_InputNew (aout, &owner->input_format, input, &input->request_vout))
172 assert (input->b_error);
174 assert (!input->b_error);
182 /*****************************************************************************
183 * aout_DecNewBuffer : ask for a new empty buffer
184 *****************************************************************************/
185 block_t *aout_DecNewBuffer (audio_output_t *aout, size_t samples)
187 /* NOTE: the caller is responsible for serializing input change */
188 aout_owner_t *owner = aout_owner (aout);
190 size_t length = samples * owner->input_format.i_bytes_per_frame
191 / owner->input_format.i_frame_length;
192 block_t *block = block_Alloc( length );
193 if( likely(block != NULL) )
195 block->i_nb_samples = samples;
196 block->i_pts = block->i_length = 0;
201 /*****************************************************************************
202 * aout_DecDeleteBuffer : destroy an undecoded buffer
203 *****************************************************************************/
204 void aout_DecDeleteBuffer (audio_output_t *aout, block_t *block)
207 aout_BufferFree (block);
210 /*****************************************************************************
211 * aout_DecPlay : filter & mix the decoded buffer
212 *****************************************************************************/
213 int aout_DecPlay (audio_output_t *p_aout, block_t *p_buffer, int i_input_rate)
215 aout_owner_t *owner = aout_owner (p_aout);
216 aout_input_t *p_input = owner->input;
218 assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
219 i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
220 assert( p_buffer->i_pts > 0 );
222 p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
223 / owner->input_format.i_rate;
226 if( p_input->b_error )
228 aout_unlock( p_aout );
229 aout_BufferFree( p_buffer );
233 aout_CheckRestart( p_aout );
234 aout_InputCheckAndRestart( p_aout, p_input );
237 p_buffer = aout_InputPlay (p_aout, p_input, p_buffer, i_input_rate,
239 if( p_buffer != NULL )
241 date_Increment (&owner->sync.date, p_buffer->i_nb_samples);
244 float amp = owner->volume.multiplier * p_input->multiplier;
245 aout_MixerRun (owner->volume.mixer, p_buffer, amp);
248 aout_OutputPlay( p_aout, p_buffer );
251 aout_unlock( p_aout );
255 int aout_DecGetResetLost (audio_output_t *aout)
257 aout_owner_t *owner = aout_owner (aout);
258 aout_input_t *input = owner->input;
262 val = input->i_buffer_lost;
263 input->i_buffer_lost = 0;
269 void aout_DecChangePause (audio_output_t *aout, bool paused, mtime_t date)
271 aout_owner_t *owner = aout_owner (aout);
274 /* XXX: Should the date be offset by the pause duration instead? */
275 date_Set (&owner->sync.date, VLC_TS_INVALID);
276 aout_OutputPause (aout, paused, date);
280 void aout_DecFlush (audio_output_t *aout)
282 aout_owner_t *owner = aout_owner (aout);
285 date_Set (&owner->sync.date, VLC_TS_INVALID);
286 aout_OutputFlush (aout, false);
290 bool aout_DecIsEmpty (audio_output_t *aout)
292 aout_owner_t *owner = aout_owner (aout);
296 /* FIXME: tell output to drain */
297 end_date = date_Get (&owner->sync.date);
299 return end_date == VLC_TS_INVALID || end_date <= mdate();
303 * Notifies the audio input of the drift from the requested audio
304 * playback timestamp (@ref block_t.i_pts) to the anticipated playback time
305 * as reported by the audio output hardware.
306 * Depending on the drift amplitude, the input core may ignore the drift
307 * trigger upsampling or downsampling, or even discard samples.
308 * Future VLC versions may instead adjust the input decoding speed.
310 * The audio output plugin is responsible for estimating the ideal current
311 * playback time defined as follows:
312 * ideal time = buffer timestamp - (output latency + pending buffer duration)
314 * Practically, this is the PTS (block_t.i_pts) of the current buffer minus
315 * the latency reported by the output programming interface.
316 * Computing the estimated drift directly would probably be more intuitive.
317 * However the use of an absolute time value does not introduce extra
318 * measurement errors due to the CPU scheduling jitter and clock resolution.
319 * Furthermore, the ideal while it is an abstract value, is easy for most
320 * audio output plugins to compute.
321 * The following definition is equivalent but depends on the clock time:
322 * ideal time = real time + drift
324 * @note If aout_LatencyReport() is never called, the core will assume that
327 * @param ideal estimated ideal time as defined above.
329 void aout_TimeReport (audio_output_t *aout, mtime_t ideal)
331 mtime_t delta = mdate() - ideal /* = -drift */;
333 aout_assert_locked (aout);
334 if (delta < -AOUT_MAX_PTS_ADVANCE || +AOUT_MAX_PTS_DELAY < delta)
336 aout_owner_t *owner = aout_owner (aout);
338 msg_Warn (aout, "not synchronized (%"PRId64" us), resampling",
340 if (date_Get (&owner->sync.date) != VLC_TS_INVALID)
341 date_Move (&owner->sync.date, delta);