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 *****************************************************************************/
34 #include <vlc_common.h>
36 #include <vlc_aout_intf.h>
38 #include <vlc_modules.h>
41 #include "aout_internal.h"
43 /*****************************************************************************
44 * aout_OutputNew : allocate a new output and rework the filter pipeline
45 *****************************************************************************
46 * This function is entered with the mixer lock.
47 *****************************************************************************/
48 int aout_OutputNew( audio_output_t *p_aout,
49 const audio_sample_format_t * p_format )
51 aout_owner_t *owner = aout_owner (p_aout);
53 aout_assert_locked( p_aout );
54 p_aout->format = *p_format;
56 /* Retrieve user defaults. */
57 int i_rate = var_InheritInteger( p_aout, "aout-rate" );
59 p_aout->format.i_rate = i_rate;
60 aout_FormatPrepare( &p_aout->format );
62 /* Find the best output plug-in. */
63 owner->module = module_need (p_aout, "audio output", "$aout", false);
64 if (owner->module == NULL)
66 msg_Err( p_aout, "no suitable audio output module" );
70 if ( var_Type( p_aout, "audio-channels" ) ==
71 (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
73 /* The user may have selected a different channels configuration. */
74 switch( var_InheritInteger( p_aout, "audio-channels" ) )
76 case AOUT_VAR_CHAN_RSTEREO:
77 p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
79 case AOUT_VAR_CHAN_STEREO:
80 p_aout->format.i_original_channels =
81 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
83 case AOUT_VAR_CHAN_LEFT:
84 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
86 case AOUT_VAR_CHAN_RIGHT:
87 p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
89 case AOUT_VAR_CHAN_DOLBYS:
90 p_aout->format.i_original_channels =
91 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
95 else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
96 && (p_aout->format.i_original_channels
97 & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
99 vlc_value_t val, text;
101 /* Mono - create the audio-channels variable. */
102 var_Create( p_aout, "audio-channels",
103 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
104 text.psz_string = _("Audio Channels");
105 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
107 val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
108 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
109 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
110 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
111 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
112 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
113 if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
115 /* Go directly to the left channel. */
116 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
117 var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
119 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
122 else if ( p_aout->format.i_physical_channels ==
123 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
124 && (p_aout->format.i_original_channels &
125 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
127 vlc_value_t val, text;
129 /* Stereo - create the audio-channels variable. */
130 var_Create( p_aout, "audio-channels",
131 VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
132 text.psz_string = _("Audio Channels");
133 var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
135 if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
137 val.i_int = AOUT_VAR_CHAN_DOLBYS;
138 text.psz_string = _("Dolby Surround");
142 val.i_int = AOUT_VAR_CHAN_STEREO;
143 text.psz_string = _("Stereo");
145 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
146 val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
147 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
148 val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
149 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
150 val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
151 var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
152 if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
154 /* Go directly to the left channel. */
155 p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
156 var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
158 var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
161 var_TriggerCallback( p_aout, "intf-change" );
163 aout_FormatPrepare( &p_aout->format );
164 aout_FormatPrint( p_aout, "output", &p_aout->format );
166 /* Choose the mixer format. */
167 owner->mixer_format = p_aout->format;
168 if (AOUT_FMT_NON_LINEAR(&p_aout->format))
169 owner->mixer_format.i_format = p_format->i_format;
171 /* Most audio filters can only deal with single-precision,
172 * so lets always use that when hardware supports floating point. */
174 owner->mixer_format.i_format = VLC_CODEC_FL32;
176 /* Otherwise, audio filters will not work. Use fixed-point if the input has
177 * more than 16-bits depth. */
178 if( p_format->i_bitspersample > 16 )
179 owner->mixer_format.i_format = VLC_CODEC_FI32;
181 /* Fallback to 16-bits. This avoids pointless conversion to and from
182 * 32-bits samples for the sole purpose of software mixing. */
183 owner->mixer_format.i_format = VLC_CODEC_S16N;
185 aout_FormatPrepare (&owner->mixer_format);
186 aout_FormatPrint (p_aout, "mixer", &owner->mixer_format);
188 /* Create filters. */
189 owner->nb_filters = 0;
190 if (aout_FiltersCreatePipeline (p_aout, owner->filters,
191 &owner->nb_filters, &owner->mixer_format,
192 &p_aout->format) < 0)
194 msg_Err( p_aout, "couldn't create audio output pipeline" );
195 module_unneed (p_aout, owner->module);
196 owner->module = NULL;
202 /*****************************************************************************
203 * aout_OutputDelete : delete the output
204 *****************************************************************************
205 * This function is entered with the mixer lock.
206 *****************************************************************************/
207 void aout_OutputDelete( audio_output_t * p_aout )
209 aout_owner_t *owner = aout_owner (p_aout);
211 aout_assert_locked( p_aout );
213 if (owner->module == NULL)
216 module_unneed (p_aout, owner->module);
217 aout_VolumeNoneInit( p_aout ); /* clear volume callback */
218 owner->module = NULL;
219 aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
222 /*****************************************************************************
223 * aout_OutputPlay : play a buffer
224 *****************************************************************************
225 * This function is entered with the mixer lock.
226 *****************************************************************************/
227 void aout_OutputPlay (audio_output_t *aout, block_t *block)
229 aout_owner_t *owner = aout_owner (aout);
231 aout_assert_locked (aout);
233 aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
236 if (block->i_buffer == 0)
238 block_Release (block);
242 aout->pf_play (aout, block);
246 * Notifies the audio output (if any) of pause/resume events.
247 * This enables the output to expedite pause, instead of waiting for its
250 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
252 aout_assert_locked( aout );
253 if( aout->pf_pause != NULL )
254 aout->pf_pause( aout, pause, date );
258 * Flushes or drains the audio output buffers.
259 * This enables the output to expedite seek and stop.
260 * @param wait if true, wait for buffer playback (i.e. drain),
261 * if false, discard the buffers immediately (i.e. flush)
263 void aout_OutputFlush( audio_output_t *aout, bool wait )
265 aout_assert_locked( aout );
267 if( aout->pf_flush != NULL )
268 aout->pf_flush( aout, wait );
272 /*** Volume handling ***/
275 * Dummy volume setter. This is the default volume setter.
277 static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
279 (void)aout; (void)volume; (void)mute;
284 * Configures the dummy volume setter.
285 * @note Audio output plugins for which volume is irrelevant
286 * should call this function during activation.
288 void aout_VolumeNoneInit (audio_output_t *aout)
290 /* aout_New() -safely- calls this function without the lock, before any
291 * other thread knows of this audio output instance.
292 aout_assert_locked (aout); */
293 aout->pf_volume_set = aout_VolumeNoneSet;
294 var_Destroy (aout, "volume");
295 var_Destroy (aout, "mute");
299 * Volume setter for software volume.
301 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
303 aout_owner_t *owner = aout_owner (aout);
305 aout_assert_locked (aout);
307 /* Cubic mapping from software volume to amplification factor.
308 * This provides a good tradeoff between low and high volume ranges.
310 * This code is only used for the VLC software mixer. If you change this
311 * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
314 volume = volume * volume * volume;
318 owner->volume.multiplier = volume;
323 * Configures the volume setter for software mixing
324 * and apply the default volume.
325 * @note Audio output plugins that cannot apply the volume
326 * should call this function during activation.
328 void aout_VolumeSoftInit (audio_output_t *aout)
330 audio_volume_t volume = var_InheritInteger (aout, "volume");
331 bool mute = var_InheritBool (aout, "mute");
333 aout_assert_locked (aout);
334 aout->pf_volume_set = aout_VolumeSoftSet;
335 aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
339 * Configures a custom volume setter. This is used by audio outputs that can
340 * control the hardware volume directly and/or emulate it internally.
341 * @param setter volume setter callback
343 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
345 aout_assert_locked (aout);
346 aout->pf_volume_set = setter;
347 var_Create (aout, "volume", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
348 var_Create (aout, "mute", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
352 * Supply or update the current custom ("hardware") volume.
353 * @note This only makes sense after calling aout_VolumeHardInit().
354 * @param setter volume setter callback
355 * @param volume current custom volume
356 * @param mute current mute flag
358 * @warning The caller (i.e. the audio output plug-in) is responsible for
359 * interlocking and synchronizing call to this function and to the
360 * audio_output_t.pf_volume_set callback. This ensures that VLC gets correct
361 * volume information (possibly with a latency).
363 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
365 audio_volume_t vol = lroundf (volume * (float)AOUT_VOLUME_DEFAULT);
367 /* We cannot acquire the volume lock as this gets called from the audio
368 * output plug-in (it would cause a lock inversion). */
369 var_SetInteger (aout, "volume", vol);
370 var_SetBool (aout, "mute", mute);
371 var_TriggerCallback (aout, "intf-change");
375 /*** Packet-oriented audio output support ***/
377 static inline aout_packet_t *aout_packet (audio_output_t *aout)
379 return (aout_packet_t *)(aout->sys);
382 void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
384 assert (p == aout_packet (aout));
386 vlc_mutex_init (&p->lock);
387 aout_FifoInit (aout, &p->partial, aout->format.i_rate);
388 aout_FifoInit (aout, &p->fifo, aout->format.i_rate);
389 p->pause_date = VLC_TS_INVALID;
390 p->samples = samples;
394 void aout_PacketDestroy (audio_output_t *aout)
396 aout_packet_t *p = aout_packet (aout);
398 aout_FifoDestroy (&p->partial);
399 aout_FifoDestroy (&p->fifo);
400 vlc_mutex_destroy (&p->lock);
403 static block_t *aout_OutputSlice (audio_output_t *);
405 void aout_PacketPlay (audio_output_t *aout, block_t *block)
407 aout_packet_t *p = aout_packet (aout);
409 vlc_mutex_lock (&p->lock);
410 aout_FifoPush (&p->partial, block);
411 while ((block = aout_OutputSlice (aout)) != NULL)
412 aout_FifoPush (&p->fifo, block);
413 vlc_mutex_unlock (&p->lock);
416 void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
418 aout_packet_t *p = aout_packet (aout);
422 assert (p->pause_date == VLC_TS_INVALID);
423 p->pause_date = date;
427 assert (p->pause_date != VLC_TS_INVALID);
429 mtime_t duration = date - p->pause_date;
431 p->pause_date = VLC_TS_INVALID;
432 vlc_mutex_lock (&p->lock);
433 aout_FifoMoveDates (&p->partial, duration);
434 aout_FifoMoveDates (&p->fifo, duration);
435 vlc_mutex_unlock (&p->lock);
439 void aout_PacketFlush (audio_output_t *aout, bool drain)
441 aout_packet_t *p = aout_packet (aout);
443 vlc_mutex_lock (&p->lock);
444 aout_FifoReset (&p->partial);
445 aout_FifoReset (&p->fifo);
446 vlc_mutex_unlock (&p->lock);
448 (void) drain; /* TODO */
453 * Rearranges audio blocks in correct number of samples.
454 * @note (FIXME) This is left here for historical reasons. It belongs in the
455 * output code. Besides, this operation should be avoided if possible.
457 static block_t *aout_OutputSlice (audio_output_t *p_aout)
459 aout_packet_t *p = aout_packet (p_aout);
460 aout_fifo_t *p_fifo = &p->partial;
461 const unsigned samples = p->samples;
462 assert( samples > 0 );
464 vlc_assert_locked( &p->lock );
466 /* Retrieve the date of the next buffer. */
467 date_t exact_start_date = p->fifo.end_date;
468 mtime_t start_date = date_Get( &exact_start_date );
470 /* See if we have enough data to prepare a new buffer for the audio output. */
471 aout_buffer_t *p_buffer = p_fifo->p_first;
472 if( p_buffer == NULL )
475 /* Find the earliest start date available. */
476 if ( start_date == VLC_TS_INVALID )
478 start_date = p_buffer->i_pts;
479 date_Set( &exact_start_date, start_date );
481 /* Compute the end date for the new buffer. */
482 mtime_t end_date = date_Increment( &exact_start_date, samples );
484 /* Check that start_date is available. */
488 /* Check for the continuity of start_date */
489 prev_date = p_buffer->i_pts + p_buffer->i_length;
490 if( prev_date >= start_date - 1 )
492 /* We authorize a +-1 because rounding errors get compensated
494 msg_Warn( p_aout, "got a packet in the past (%"PRId64")",
495 start_date - prev_date );
496 aout_BufferFree( aout_FifoPop( p_fifo ) );
498 p_buffer = p_fifo->p_first;
499 if( p_buffer == NULL )
503 /* Check that we have enough samples. */
504 while( prev_date < end_date )
506 p_buffer = p_buffer->p_next;
507 if( p_buffer == NULL )
510 /* Check that all buffers are contiguous. */
511 if( prev_date != p_buffer->i_pts )
514 "buffer hole, dropping packets (%"PRId64")",
515 p_buffer->i_pts - prev_date );
517 aout_buffer_t *p_deleted;
518 while( (p_deleted = p_fifo->p_first) != p_buffer )
519 aout_BufferFree( aout_FifoPop( p_fifo ) );
522 prev_date = p_buffer->i_pts + p_buffer->i_length;
525 if( !AOUT_FMT_NON_LINEAR( &p_aout->format ) )
527 p_buffer = p_fifo->p_first;
529 /* Additionally check that p_first_byte_to_mix is well located. */
530 const unsigned framesize = p_aout->format.i_bytes_per_frame;
531 ssize_t delta = (start_date - p_buffer->i_pts)
532 * p_aout->format.i_rate / CLOCK_FREQ;
534 msg_Warn( p_aout, "input start is not output end (%zd)", delta );
537 /* Is it really the best way to do it ? */
538 aout_FifoReset (&p->fifo);
543 mtime_t t = delta * CLOCK_FREQ / p_aout->format.i_rate;
544 p_buffer->i_nb_samples -= delta;
545 p_buffer->i_pts += t;
546 p_buffer->i_length -= t;
548 p_buffer->p_buffer += delta;
549 p_buffer->i_buffer -= delta;
552 /* Build packet with adequate number of samples */
553 unsigned needed = samples * framesize;
554 p_buffer = block_Alloc( needed );
555 if( unlikely(p_buffer == NULL) )
556 /* XXX: should free input buffers */
558 p_buffer->i_nb_samples = samples;
560 for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
562 aout_buffer_t *p_inbuf = p_fifo->p_first;
563 if( unlikely(p_inbuf == NULL) )
565 msg_Err( p_aout, "packetization error" );
566 vlc_memset( p_out, 0, needed );
570 const uint8_t *p_in = p_inbuf->p_buffer;
571 size_t avail = p_inbuf->i_nb_samples * framesize;
574 vlc_memcpy( p_out, p_in, needed );
575 p_fifo->p_first->p_buffer += needed;
576 p_fifo->p_first->i_buffer -= needed;
578 p_fifo->p_first->i_nb_samples -= needed;
580 mtime_t t = needed * CLOCK_FREQ / p_aout->format.i_rate;
581 p_fifo->p_first->i_pts += t;
582 p_fifo->p_first->i_length -= t;
586 vlc_memcpy( p_out, p_in, avail );
590 aout_BufferFree( aout_FifoPop( p_fifo ) );
594 p_buffer = aout_FifoPop( p_fifo );
596 p_buffer->i_pts = start_date;
597 p_buffer->i_length = end_date - start_date;
602 /*****************************************************************************
603 * aout_OutputNextBuffer : give the audio output plug-in the right buffer
604 *****************************************************************************
605 * If b_can_sleek is 1, the aout core functions won't try to resample
606 * new buffers to catch up - that is we suppose that the output plug-in can
607 * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
608 * This function is entered with no lock at all :-).
609 *****************************************************************************/
610 aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
614 aout_packet_t *p = aout_packet (p_aout);
615 aout_fifo_t *p_fifo = &p->fifo;
616 aout_buffer_t *p_buffer = NULL;
617 mtime_t now = mdate();
619 vlc_mutex_lock( &p->lock );
620 if( p->pause_date != VLC_TS_INVALID )
623 /* Drop the audio sample if the audio output is really late.
624 * In the case of b_can_sleek, we don't use a resampler so we need to be
625 * a lot more severe. */
626 while( ((p_buffer = p_fifo->p_first) != NULL)
627 && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
629 msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
630 "trashing %"PRId64"us", now - p_buffer->i_pts,
631 p_buffer->i_length );
632 aout_BufferFree( aout_FifoPop( p_fifo ) );
635 if( p_buffer == NULL )
637 #if 0 /* This is bad because the audio output might just be trying to fill
638 * in its internal buffers. And anyway, it's up to the audio output
639 * to deal with this kind of starvation. */
641 /* Set date to 0, to allow the mixer to send a new buffer ASAP */
642 aout_FifoReset( &p->fifo );
645 "audio output is starving (no input), playing silence" );
646 p_aout->starving = true;
651 mtime_t delta = start_date - p_buffer->i_pts;
652 /* Here we suppose that all buffers have the same duration - this is
653 * generally true, and anyway if it's wrong it won't be a disaster.
655 if ( 0 > delta + p_buffer->i_length )
658 msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
659 "playing silence", -delta );
666 p_buffer = aout_FifoPop( p_fifo );
669 && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
671 /* Try to compensate the drift by doing some resampling. */
672 msg_Warn( p_aout, "output date isn't PTS date, requesting "
673 "resampling (%"PRId64")", delta );
675 aout_FifoMoveDates (&p->partial, delta);
676 aout_FifoMoveDates (p_fifo, delta);
677 #warning FIXME: feed back to input for resampling!!!
680 vlc_mutex_unlock( &p->lock );