1 /*****************************************************************************
2 * dec.c : audio output API towards decoders
3 *****************************************************************************
4 * Copyright (C) 2002-2007 VLC authors and VideoLAN
7 * Authors: Christophe Massiot <massiot@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
33 #include <vlc_common.h>
35 #include <vlc_input.h>
37 #include "aout_internal.h"
41 * Creates an audio output
43 int aout_DecNew( audio_output_t
*p_aout
,
44 const audio_sample_format_t
*p_format
,
45 const audio_replay_gain_t
*p_replay_gain
,
46 const aout_request_vout_t
*p_request_vout
)
48 /* Sanitize audio format */
49 unsigned i_channels
= aout_FormatNbChannels( p_format
);
50 if( i_channels
!= p_format
->i_channels
&& AOUT_FMT_LINEAR( p_format
) )
52 msg_Err( p_aout
, "incompatible audio channels count with layout mask" );
56 if( p_format
->i_rate
> 352800 )
58 msg_Err( p_aout
, "excessive audio sample frequency (%u)",
62 if( p_format
->i_rate
< 4000 )
64 msg_Err( p_aout
, "too low audio sample frequency (%u)",
69 var_Create (p_aout
, "stereo-mode", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
71 txt
.psz_string
= _("Stereo audio mode");
72 var_Change (p_aout
, "stereo-mode", VLC_VAR_SETTEXT
, &txt
, NULL
);
74 aout_owner_t
*owner
= aout_owner(p_aout
);
76 /* TODO: reduce lock scope depending on decoder's real need */
77 aout_OutputLock (p_aout
);
79 /* Create the audio output stream */
80 owner
->volume
= aout_volume_New (p_aout
, p_replay_gain
);
82 atomic_store (&owner
->restart
, 0);
83 owner
->input_format
= *p_format
;
84 owner
->mixer_format
= owner
->input_format
;
85 owner
->request_vout
= *p_request_vout
;
87 if (aout_OutputNew (p_aout
, &owner
->mixer_format
))
89 aout_volume_SetFormat (owner
->volume
, owner
->mixer_format
.i_format
);
91 /* Create the audio filtering "input" pipeline */
92 owner
->filters
= aout_FiltersNew (p_aout
, p_format
, &owner
->mixer_format
,
93 &owner
->request_vout
);
94 if (owner
->filters
== NULL
)
96 aout_OutputDelete (p_aout
);
98 aout_volume_Delete (owner
->volume
);
100 aout_OutputUnlock (p_aout
);
101 var_Destroy (p_aout
, "stereo-mode");
105 owner
->sync
.end
= VLC_TS_INVALID
;
106 owner
->sync
.resamp_type
= AOUT_RESAMPLING_NONE
;
107 owner
->sync
.discontinuity
= true;
108 aout_OutputUnlock (p_aout
);
110 atomic_init (&owner
->buffers_lost
, 0);
111 atomic_init (&owner
->buffers_played
, 0);
116 * Stops all plugins involved in the audio output.
118 void aout_DecDelete (audio_output_t
*aout
)
120 aout_owner_t
*owner
= aout_owner (aout
);
122 aout_OutputLock (aout
);
123 if (owner
->mixer_format
.i_format
)
125 aout_FiltersDelete (aout
, owner
->filters
);
126 aout_OutputDelete (aout
);
128 aout_volume_Delete (owner
->volume
);
129 owner
->volume
= NULL
;
130 aout_OutputUnlock (aout
);
131 var_Destroy (aout
, "stereo-mode");
134 static int aout_CheckReady (audio_output_t
*aout
)
136 aout_owner_t
*owner
= aout_owner (aout
);
138 int status
= AOUT_DEC_SUCCESS
;
139 int restart
= atomic_exchange (&owner
->restart
, 0);
140 if (unlikely(restart
))
142 if (owner
->mixer_format
.i_format
)
143 aout_FiltersDelete (aout
, owner
->filters
);
145 if (restart
& AOUT_RESTART_OUTPUT
)
146 { /* Reinitializes the output */
147 msg_Dbg (aout
, "restarting output...");
148 if (owner
->mixer_format
.i_format
)
149 aout_OutputDelete (aout
);
150 owner
->mixer_format
= owner
->input_format
;
151 if (aout_OutputNew (aout
, &owner
->mixer_format
))
152 owner
->mixer_format
.i_format
= 0;
153 aout_volume_SetFormat (owner
->volume
,
154 owner
->mixer_format
.i_format
);
155 status
= AOUT_DEC_CHANGED
;
158 msg_Dbg (aout
, "restarting filters...");
159 owner
->sync
.end
= VLC_TS_INVALID
;
160 owner
->sync
.resamp_type
= AOUT_RESAMPLING_NONE
;
162 if (owner
->mixer_format
.i_format
)
164 owner
->filters
= aout_FiltersNew (aout
, &owner
->input_format
,
165 &owner
->mixer_format
,
166 &owner
->request_vout
);
167 if (owner
->filters
== NULL
)
169 aout_OutputDelete (aout
);
170 owner
->mixer_format
.i_format
= 0;
173 /* TODO: This would be a good time to call clean up any video output
174 * left over by an audio visualization:
175 input_resource_TerminatVout(MAGIC HERE); */
177 return (owner
->mixer_format
.i_format
) ? status
: AOUT_DEC_FAILED
;
181 * Marks the audio output for restart, to update any parameter of the output
182 * plug-in (e.g. output device or channel mapping).
184 void aout_RequestRestart (audio_output_t
*aout
, unsigned mode
)
186 aout_owner_t
*owner
= aout_owner (aout
);
187 atomic_fetch_or (&owner
->restart
, mode
);
188 msg_Dbg (aout
, "restart requested (%u)", mode
);
195 static void aout_StopResampling (audio_output_t
*aout
)
197 aout_owner_t
*owner
= aout_owner (aout
);
199 owner
->sync
.resamp_type
= AOUT_RESAMPLING_NONE
;
200 aout_FiltersAdjustResampling (owner
->filters
, 0);
203 static void aout_DecSilence (audio_output_t
*aout
, mtime_t length
, mtime_t pts
)
205 aout_owner_t
*owner
= aout_owner (aout
);
206 const audio_sample_format_t
*fmt
= &owner
->mixer_format
;
207 size_t frames
= (fmt
->i_rate
* length
) / CLOCK_FREQ
;
209 block_t
*block
= block_Alloc (frames
* fmt
->i_bytes_per_frame
210 / fmt
->i_frame_length
);
211 if (unlikely(block
== NULL
))
214 msg_Dbg (aout
, "inserting %zu zeroes", frames
);
215 memset (block
->p_buffer
, 0, block
->i_buffer
);
216 block
->i_nb_samples
= frames
;
219 block
->i_length
= length
;
220 aout_OutputPlay (aout
, block
);
223 static void aout_DecSynchronize (audio_output_t
*aout
, mtime_t dec_pts
,
226 aout_owner_t
*owner
= aout_owner (aout
);
230 * Depending on the drift between the actual and intended playback times,
231 * the audio core may ignore the drift, trigger upsampling or downsampling,
232 * insert silence or even discard samples.
233 * Future VLC versions may instead adjust the input rate.
235 * The audio output plugin is responsible for estimating its actual
236 * playback time, or rather the estimated time when the next sample will
237 * be played. (The actual playback time is always the current time, that is
238 * to say mdate(). It is not an useful statistic.)
240 * Most audio output plugins can estimate the delay until playback of
241 * the next sample to be written to the buffer, or equally the time until
242 * all samples in the buffer will have been played. Then:
243 * pts = mdate() + delay
245 if (aout_OutputTimeGet (aout
, &drift
) != 0)
246 return; /* nothing can be done if timing is unknown */
247 drift
+= mdate () - dec_pts
;
249 /* Late audio output.
250 * This can happen due to insufficient caching, scheduling jitter
251 * or bug in the decoder. Ideally, the output would seek backward. But that
252 * is not portable, not supported by some hardware and often unsafe/buggy
253 * where supported. The other alternative is to flush the buffers
255 if (drift
> (owner
->sync
.discontinuity
? 0
256 : +3 * input_rate
* AOUT_MAX_PTS_DELAY
/ INPUT_RATE_DEFAULT
))
258 if (!owner
->sync
.discontinuity
)
259 msg_Warn (aout
, "playback way too late (%"PRId64
"): "
260 "flushing buffers", drift
);
262 msg_Dbg (aout
, "playback too late (%"PRId64
"): "
263 "flushing buffers", drift
);
264 aout_OutputFlush (aout
, false);
266 aout_StopResampling (aout
);
267 owner
->sync
.end
= VLC_TS_INVALID
;
268 owner
->sync
.discontinuity
= true;
270 /* Now the output might be too early... Recheck. */
271 if (aout_OutputTimeGet (aout
, &drift
) != 0)
272 return; /* nothing can be done if timing is unknown */
273 drift
+= mdate () - dec_pts
;
276 /* Early audio output.
277 * This is rare except at startup when the buffers are still empty. */
278 if (drift
< (owner
->sync
.discontinuity
? 0
279 : -3 * input_rate
* AOUT_MAX_PTS_ADVANCE
/ INPUT_RATE_DEFAULT
))
281 if (!owner
->sync
.discontinuity
)
282 msg_Warn (aout
, "playback way too early (%"PRId64
"): "
283 "playing silence", drift
);
284 aout_DecSilence (aout
, -drift
, dec_pts
);
286 aout_StopResampling (aout
);
287 owner
->sync
.discontinuity
= true;
291 if (!aout_FiltersCanResample(owner
->filters
))
295 if (drift
> +AOUT_MAX_PTS_DELAY
296 && owner
->sync
.resamp_type
!= AOUT_RESAMPLING_UP
)
298 msg_Warn (aout
, "playback too late (%"PRId64
"): up-sampling",
300 owner
->sync
.resamp_type
= AOUT_RESAMPLING_UP
;
301 owner
->sync
.resamp_start_drift
= +drift
;
303 if (drift
< -AOUT_MAX_PTS_ADVANCE
304 && owner
->sync
.resamp_type
!= AOUT_RESAMPLING_DOWN
)
306 msg_Warn (aout
, "playback too early (%"PRId64
"): down-sampling",
308 owner
->sync
.resamp_type
= AOUT_RESAMPLING_DOWN
;
309 owner
->sync
.resamp_start_drift
= -drift
;
312 if (owner
->sync
.resamp_type
== AOUT_RESAMPLING_NONE
)
313 return; /* Everything is fine. Nothing to do. */
315 if (llabs (drift
) > 2 * owner
->sync
.resamp_start_drift
)
316 { /* If the drift is ever increasing, then something is seriously wrong.
317 * Cease resampling and hope for the best. */
318 msg_Warn (aout
, "timing screwed (drift: %"PRId64
" us): "
319 "stopping resampling", drift
);
320 aout_StopResampling (aout
);
324 /* Resampling has been triggered earlier. This checks if it needs to be
325 * increased or decreased. Resampling rate changes must be kept slow for
326 * the comfort of listeners. */
327 int adj
= (owner
->sync
.resamp_type
== AOUT_RESAMPLING_UP
) ? +2 : -2;
329 if (2 * llabs (drift
) <= owner
->sync
.resamp_start_drift
)
330 /* If the drift has been reduced from more than half its initial
331 * value, then it is time to switch back the resampling direction. */
334 if (!aout_FiltersAdjustResampling (owner
->filters
, adj
))
335 { /* Everything is back to normal: stop resampling. */
336 owner
->sync
.resamp_type
= AOUT_RESAMPLING_NONE
;
337 msg_Dbg (aout
, "resampling stopped (drift: %"PRId64
" us)", drift
);
341 /*****************************************************************************
342 * aout_DecPlay : filter & mix the decoded buffer
343 *****************************************************************************/
344 int aout_DecPlay (audio_output_t
*aout
, block_t
*block
, int input_rate
)
346 aout_owner_t
*owner
= aout_owner (aout
);
348 assert (input_rate
>= INPUT_RATE_DEFAULT
/ AOUT_MAX_INPUT_RATE
);
349 assert (input_rate
<= INPUT_RATE_DEFAULT
* AOUT_MAX_INPUT_RATE
);
350 assert (block
->i_pts
>= VLC_TS_0
);
352 block
->i_length
= CLOCK_FREQ
* block
->i_nb_samples
353 / owner
->input_format
.i_rate
;
355 aout_OutputLock (aout
);
356 int ret
= aout_CheckReady (aout
);
357 if (unlikely(ret
== AOUT_DEC_FAILED
))
358 goto drop
; /* Pipeline is unrecoverably broken :-( */
360 const mtime_t now
= mdate (), advance
= block
->i_pts
- now
;
361 if (advance
< -AOUT_MAX_PTS_DELAY
)
362 { /* Late buffer can be caused by bugs in the decoder, by scheduling
363 * latency spikes (excessive load, SIGSTOP, etc.) or if buffering is
364 * insufficient. We assume the PTS is wrong and play the buffer anyway:
365 * Hopefully video has encountered a similar PTS problem as audio. */
366 msg_Warn (aout
, "buffer too late (%"PRId64
" us): dropped", advance
);
369 if (advance
> AOUT_MAX_ADVANCE_TIME
)
370 { /* Early buffers can only be caused by bugs in the decoder. */
371 msg_Err (aout
, "buffer too early (%"PRId64
" us): dropped", advance
);
374 if (block
->i_flags
& BLOCK_FLAG_DISCONTINUITY
)
375 owner
->sync
.discontinuity
= true;
377 block
= aout_FiltersPlay (owner
->filters
, block
, input_rate
);
381 /* Software volume */
382 aout_volume_Amplify (owner
->volume
, block
);
384 /* Drift correction */
385 aout_DecSynchronize (aout
, block
->i_pts
, input_rate
);
388 owner
->sync
.end
= block
->i_pts
+ block
->i_length
+ 1;
389 owner
->sync
.discontinuity
= false;
390 aout_OutputPlay (aout
, block
);
391 atomic_fetch_add(&owner
->buffers_played
, 1);
393 aout_OutputUnlock (aout
);
396 owner
->sync
.discontinuity
= true;
397 block_Release (block
);
399 atomic_fetch_add(&owner
->buffers_lost
, 1);
403 void aout_DecGetResetStats(audio_output_t
*aout
, unsigned *restrict lost
,
404 unsigned *restrict played
)
406 aout_owner_t
*owner
= aout_owner (aout
);
408 *lost
= atomic_exchange(&owner
->buffers_lost
, 0);
409 *played
= atomic_exchange(&owner
->buffers_played
, 0);
412 void aout_DecChangePause (audio_output_t
*aout
, bool paused
, mtime_t date
)
414 aout_owner_t
*owner
= aout_owner (aout
);
416 aout_OutputLock (aout
);
417 if (owner
->sync
.end
!= VLC_TS_INVALID
)
420 owner
->sync
.end
-= date
;
422 owner
->sync
.end
+= date
;
424 if (owner
->mixer_format
.i_format
)
425 aout_OutputPause (aout
, paused
, date
);
426 aout_OutputUnlock (aout
);
429 void aout_DecFlush (audio_output_t
*aout
, bool wait
)
431 aout_owner_t
*owner
= aout_owner (aout
);
433 aout_OutputLock (aout
);
434 owner
->sync
.end
= VLC_TS_INVALID
;
435 if (owner
->mixer_format
.i_format
)
439 block_t
*block
= aout_FiltersDrain (owner
->filters
);
441 aout_OutputPlay (aout
, block
);
444 aout_FiltersFlush (owner
->filters
);
445 aout_OutputFlush (aout
, wait
);
447 aout_OutputUnlock (aout
);