1 /*****************************************************************************
2 * output.c : internal management of output streams for the audio output
3 *****************************************************************************
4 * Copyright (C) 2002-2004 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 *****************************************************************************/
32 #include <vlc_common.h>
34 #include <vlc_aout_intf.h>
36 #include <vlc_modules.h>
39 #include "aout_internal.h"
41 /*****************************************************************************
42 * aout_OutputNew : allocate a new output and rework the filter pipeline
43 *****************************************************************************
44 * This function is entered with the mixer lock.
45 *****************************************************************************/
46 int aout_OutputNew( audio_output_t *p_aout,
47 const audio_sample_format_t * p_format )
49 aout_owner_t *owner = aout_owner (p_aout);
51 aout_assert_locked( p_aout );
52 p_aout->format = *p_format;
54 /* Retrieve user defaults. */
55 int i_rate = var_InheritInteger( p_aout, "aout-rate" );
57 p_aout->format.i_rate = i_rate;
58 aout_FormatPrepare( &p_aout->format );
60 /* Find the best output plug-in. */
61 owner->module = module_need (p_aout, "audio output", "$aout", false);
62 if (owner->module == NULL)
64 msg_Err( p_aout, "no suitable audio output module" );
68 if ( var_Type( p_aout, "audio-channels" ) ==
69 (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
71 /* The user may have selected a different channels configuration. */
72 switch( var_InheritInteger( p_aout, "audio-channels" ) )
74 case AOUT_VAR_CHAN_RSTEREO:
75 p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
77 case AOUT_VAR_CHAN_STEREO:
78 p_aout->format.i_original_channels =
79 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
81 case AOUT_VAR_CHAN_LEFT:
82 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
84 case AOUT_VAR_CHAN_RIGHT:
85 p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
87 case AOUT_VAR_CHAN_DOLBYS:
88 p_aout->format.i_original_channels =
89 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
93 else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
94 && (p_aout->format.i_original_channels
95 & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
97 vlc_value_t val, text;
99 /* Mono - create the audio-channels variable. */
100 var_Create( p_aout, "audio-channels",
101 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
102 text.psz_string = _("Audio Channels");
103 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
105 val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
106 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
107 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
108 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
109 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
110 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
111 if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
113 /* Go directly to the left channel. */
114 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
115 var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
117 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
120 else if ( p_aout->format.i_physical_channels ==
121 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
122 && (p_aout->format.i_original_channels &
123 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
125 vlc_value_t val, text;
127 /* Stereo - create the audio-channels variable. */
128 var_Create( p_aout, "audio-channels",
129 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
130 text.psz_string = _("Audio Channels");
131 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
133 if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
135 val.i_int = AOUT_VAR_CHAN_DOLBYS;
136 text.psz_string = _("Dolby Surround");
140 val.i_int = AOUT_VAR_CHAN_STEREO;
141 text.psz_string = _("Stereo");
143 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
144 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
145 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
146 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
147 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
148 val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
149 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
150 if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
152 /* Go directly to the left channel. */
153 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
154 var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
156 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
159 var_TriggerCallback( p_aout, "intf-change" );
161 aout_FormatPrepare( &p_aout->format );
162 aout_FormatPrint( p_aout, "output", &p_aout->format );
164 /* Choose the mixer format. */
165 owner->mixer_format = p_aout->format;
166 if (AOUT_FMT_NON_LINEAR(&p_aout->format))
167 owner->mixer_format.i_format = p_format->i_format;
169 /* Most audio filters can only deal with single-precision,
170 * so lets always use that when hardware supports floating point. */
172 owner->mixer_format.i_format = VLC_CODEC_FL32;
174 /* Otherwise, audio filters will not work. Use fixed-point if the input has
175 * more than 16-bits depth. */
176 if( p_format->i_bitspersample > 16 )
177 owner->mixer_format.i_format = VLC_CODEC_FI32;
179 /* Fallback to 16-bits. This avoids pointless conversion to and from
180 * 32-bits samples for the sole purpose of software mixing. */
181 owner->mixer_format.i_format = VLC_CODEC_S16N;
183 aout_FormatPrepare (&owner->mixer_format);
184 aout_FormatPrint (p_aout, "mixer", &owner->mixer_format);
186 /* Create filters. */
187 owner->nb_filters = 0;
188 if (aout_FiltersCreatePipeline (p_aout, owner->filters,
189 &owner->nb_filters, &owner->mixer_format,
190 &p_aout->format) < 0)
192 msg_Err( p_aout, "couldn't create audio output pipeline" );
193 module_unneed (p_aout, owner->module);
194 owner->module = NULL;
200 /*****************************************************************************
201 * aout_OutputDelete : delete the output
202 *****************************************************************************
203 * This function is entered with the mixer lock.
204 *****************************************************************************/
205 void aout_OutputDelete( audio_output_t * p_aout )
207 aout_owner_t *owner = aout_owner (p_aout);
209 aout_assert_locked( p_aout );
211 if (owner->module == NULL)
214 module_unneed (p_aout, owner->module);
215 aout_VolumeNoneInit( p_aout ); /* clear volume callback */
216 owner->module = NULL;
217 aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
220 /*****************************************************************************
221 * aout_OutputPlay : play a buffer
222 *****************************************************************************
223 * This function is entered with the mixer lock.
224 *****************************************************************************/
225 void aout_OutputPlay (audio_output_t *aout, block_t *block)
227 aout_owner_t *owner = aout_owner (aout);
229 aout_assert_locked (aout);
231 aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
234 if (block->i_buffer == 0)
236 block_Release (block);
240 aout->pf_play (aout, block);
244 * Notifies the audio output (if any) of pause/resume events.
245 * This enables the output to expedite pause, instead of waiting for its
248 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
250 aout_assert_locked( aout );
251 if( aout->pf_pause != NULL )
252 aout->pf_pause( aout, pause, date );
256 * Flushes or drains the audio output buffers.
257 * This enables the output to expedite seek and stop.
258 * @param wait if true, wait for buffer playback (i.e. drain),
259 * if false, discard the buffers immediately (i.e. flush)
261 void aout_OutputFlush( audio_output_t *aout, bool wait )
263 aout_assert_locked( aout );
265 if( aout->pf_flush != NULL )
266 aout->pf_flush( aout, wait );
270 /*** Volume handling ***/
273 * Dummy volume setter. This is the default volume setter.
275 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
277 (void)aout; (void)volume; (void)mute;
282 * Configures the dummy volume setter.
283 * @note Audio output plugins for which volume is irrelevant
284 * should call this function during activation.
286 void aout_VolumeNoneInit (audio_output_t *aout)
288 /* aout_New() -safely- calls this function without the lock, before any
289 * other thread knows of this audio output instance.
290 aout_assert_locked (aout); */
291 aout->pf_volume_set = aout_VolumeNoneSet;
295 * Volume setter for software volume.
297 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
299 aout_owner_t *owner = aout_owner (aout);
301 aout_assert_locked (aout);
303 /* Cubic mapping from software volume to amplification factor.
304 * This provides a good tradeoff between low and high volume ranges.
306 * This code is only used for the VLC software mixer. If you change this
307 * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
310 volume = volume * volume * volume;
314 owner->volume.multiplier = volume;
319 * Configures the volume setter for software mixing
320 * and apply the default volume.
321 * @note Audio output plugins that cannot apply the volume
322 * should call this function during activation.
324 void aout_VolumeSoftInit (audio_output_t *aout)
326 audio_volume_t volume = var_InheritInteger (aout, "volume");
327 bool mute = var_InheritBool (aout, "mute");
329 aout_assert_locked (aout);
330 aout->pf_volume_set = aout_VolumeSoftSet;
331 aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
335 * Configures a custom volume setter. This is used by audio outputs that can
336 * control the hardware volume directly and/or emulate it internally.
337 * @param setter volume setter callback
339 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
341 aout_assert_locked (aout);
342 aout->pf_volume_set = setter;
346 * Supply or update the current custom ("hardware") volume.
347 * @note This only makes sense after calling aout_VolumeHardInit().
348 * @param setter volume setter callback
349 * @param volume current custom volume
350 * @param mute current mute flag
351 * @note Audio output plugins that cannot apply the volume
352 * should call this function during activation.
354 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
357 /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
358 * called from the audio output (it would cause a lock inversion).
359 * We also should not override the input manager volume, but only the
360 * volume of the current audio output... FIXME */
361 msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
365 /*** Packet-oriented audio output support ***/
367 static inline aout_packet_t *aout_packet (audio_output_t *aout)
369 return (aout_packet_t *)(aout->sys);
372 void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
374 assert (p == aout_packet (aout));
376 aout_FifoInit (aout, &p->partial, aout->format.i_rate);
377 aout_FifoInit (aout, &p->fifo, aout->format.i_rate);
378 p->pause_date = VLC_TS_INVALID;
379 p->samples = samples;
383 void aout_PacketDestroy (audio_output_t *aout)
385 aout_packet_t *p = aout_packet (aout);
387 aout_FifoDestroy (&p->partial);
388 aout_FifoDestroy (&p->fifo);
391 static block_t *aout_OutputSlice (audio_output_t *);
393 void aout_PacketPlay (audio_output_t *aout, block_t *block)
395 aout_packet_t *p = aout_packet (aout);
397 aout_FifoPush (&p->partial, block);
398 while ((block = aout_OutputSlice (aout)) != NULL)
399 aout_FifoPush (&p->fifo, block);
402 void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
404 aout_packet_t *p = aout_packet (aout);
408 assert (p->pause_date == VLC_TS_INVALID);
409 p->pause_date = date;
413 assert (p->pause_date != VLC_TS_INVALID);
415 mtime_t duration = date - p->pause_date;
417 p->pause_date = VLC_TS_INVALID;
418 aout_FifoMoveDates (&p->partial, duration);
419 aout_FifoMoveDates (&p->fifo, duration);
423 void aout_PacketFlush (audio_output_t *aout, bool drain)
425 aout_packet_t *p = aout_packet (aout);
427 aout_FifoReset (&p->partial);
428 aout_FifoReset (&p->fifo);
429 (void) drain; /* TODO */
434 * Rearranges audio blocks in correct number of samples.
435 * @note (FIXME) This is left here for historical reasons. It belongs in the
436 * output code. Besides, this operation should be avoided if possible.
438 static block_t *aout_OutputSlice (audio_output_t *p_aout)
440 aout_packet_t *p = aout_packet (p_aout);
441 aout_fifo_t *p_fifo = &p->partial;
442 const unsigned samples = p->samples;
443 assert( samples > 0 );
445 aout_assert_locked( p_aout );
447 /* Retrieve the date of the next buffer. */
448 date_t exact_start_date = p->fifo.end_date;
449 mtime_t start_date = date_Get( &exact_start_date );
451 /* See if we have enough data to prepare a new buffer for the audio output. */
452 aout_buffer_t *p_buffer = p_fifo->p_first;
453 if( p_buffer == NULL )
456 /* Find the earliest start date available. */
457 if ( start_date == VLC_TS_INVALID )
459 start_date = p_buffer->i_pts;
460 date_Set( &exact_start_date, start_date );
462 /* Compute the end date for the new buffer. */
463 mtime_t end_date = date_Increment( &exact_start_date, samples );
465 /* Check that start_date is available. */
469 /* Check for the continuity of start_date */
470 prev_date = p_buffer->i_pts + p_buffer->i_length;
471 if( prev_date >= start_date - 1 )
473 /* We authorize a +-1 because rounding errors get compensated
475 msg_Warn( p_aout, "got a packet in the past (%"PRId64")",
476 start_date - prev_date );
477 aout_BufferFree( aout_FifoPop( p_fifo ) );
479 p_buffer = p_fifo->p_first;
480 if( p_buffer == NULL )
484 /* Check that we have enough samples. */
485 while( prev_date < end_date )
487 p_buffer = p_buffer->p_next;
488 if( p_buffer == NULL )
491 /* Check that all buffers are contiguous. */
492 if( prev_date != p_buffer->i_pts )
495 "buffer hole, dropping packets (%"PRId64")",
496 p_buffer->i_pts - prev_date );
498 aout_buffer_t *p_deleted;
499 while( (p_deleted = p_fifo->p_first) != p_buffer )
500 aout_BufferFree( aout_FifoPop( p_fifo ) );
503 prev_date = p_buffer->i_pts + p_buffer->i_length;
506 if( !AOUT_FMT_NON_LINEAR( &p_aout->format ) )
508 p_buffer = p_fifo->p_first;
510 /* Additionally check that p_first_byte_to_mix is well located. */
511 const unsigned framesize = p_aout->format.i_bytes_per_frame;
512 ssize_t delta = (start_date - p_buffer->i_pts)
513 * p_aout->format.i_rate / CLOCK_FREQ;
515 msg_Warn( p_aout, "input start is not output end (%zd)", delta );
518 /* Is it really the best way to do it ? */
519 aout_FifoReset (&p->fifo);
524 mtime_t t = delta * CLOCK_FREQ / p_aout->format.i_rate;
525 p_buffer->i_nb_samples -= delta;
526 p_buffer->i_pts += t;
527 p_buffer->i_length -= t;
529 p_buffer->p_buffer += delta;
530 p_buffer->i_buffer -= delta;
533 /* Build packet with adequate number of samples */
534 unsigned needed = samples * framesize;
535 p_buffer = block_Alloc( needed );
536 if( unlikely(p_buffer == NULL) )
537 /* XXX: should free input buffers */
539 p_buffer->i_nb_samples = samples;
541 for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
543 aout_buffer_t *p_inbuf = p_fifo->p_first;
544 if( unlikely(p_inbuf == NULL) )
546 msg_Err( p_aout, "packetization error" );
547 vlc_memset( p_out, 0, needed );
551 const uint8_t *p_in = p_inbuf->p_buffer;
552 size_t avail = p_inbuf->i_nb_samples * framesize;
555 vlc_memcpy( p_out, p_in, needed );
556 p_fifo->p_first->p_buffer += needed;
557 p_fifo->p_first->i_buffer -= needed;
559 p_fifo->p_first->i_nb_samples -= needed;
561 mtime_t t = needed * CLOCK_FREQ / p_aout->format.i_rate;
562 p_fifo->p_first->i_pts += t;
563 p_fifo->p_first->i_length -= t;
567 vlc_memcpy( p_out, p_in, avail );
571 aout_BufferFree( aout_FifoPop( p_fifo ) );
575 p_buffer = aout_FifoPop( p_fifo );
577 p_buffer->i_pts = start_date;
578 p_buffer->i_length = end_date - start_date;
583 /*****************************************************************************
584 * aout_OutputNextBuffer : give the audio output plug-in the right buffer
585 *****************************************************************************
586 * If b_can_sleek is 1, the aout core functions won't try to resample
587 * new buffers to catch up - that is we suppose that the output plug-in can
588 * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
589 * This function is entered with no lock at all :-).
590 *****************************************************************************/
591 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
595 aout_packet_t *p = aout_packet (p_aout);
596 aout_fifo_t *p_fifo = &p->fifo;
597 aout_buffer_t * p_buffer;
598 mtime_t now = mdate();
602 /* Drop the audio sample if the audio output is really late.
603 * In the case of b_can_sleek, we don't use a resampler so we need to be
604 * a lot more severe. */
605 while( ((p_buffer = p_fifo->p_first) != NULL)
606 && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
608 msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
609 "trashing %"PRId64"us", now - p_buffer->i_pts,
610 p_buffer->i_length );
611 aout_BufferFree( aout_FifoPop( p_fifo ) );
614 if( p_buffer == NULL )
616 #if 0 /* This is bad because the audio output might just be trying to fill
617 * in its internal buffers. And anyway, it's up to the audio output
618 * to deal with this kind of starvation. */
620 /* Set date to 0, to allow the mixer to send a new buffer ASAP */
621 aout_FifoReset( &p->fifo );
624 "audio output is starving (no input), playing silence" );
625 p_aout->starving = true;
630 mtime_t delta = start_date - p_buffer->i_pts;
631 /* Here we suppose that all buffers have the same duration - this is
632 * generally true, and anyway if it's wrong it won't be a disaster.
634 if ( 0 > delta + p_buffer->i_length )
637 msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
638 "playing silence", -delta );
645 p_buffer = aout_FifoPop( p_fifo );
648 && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
650 /* Try to compensate the drift by doing some resampling. */
651 msg_Warn( p_aout, "output date isn't PTS date, requesting "
652 "resampling (%"PRId64")", delta );
654 aout_FifoMoveDates (&p->partial, delta);
655 aout_FifoMoveDates (p_fifo, delta);
658 aout_unlock( p_aout );