]> git.sesse.net Git - vlc/blobdiff - src/audio_output/audio_output.c
* configure.in: Fixed detection of Qt-embedded.
[vlc] / src / audio_output / audio_output.c
index 3f0e4b02faa0bdde7ad7adb71c877a3d9f346110..d070fc984ea97472f7da8a0b14a62df94542dd6f 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
- * audio_output.c : audio output instance
+ * audio_output.c : audio output instance miscellaneous functions
  *****************************************************************************
  * Copyright (C) 2002 VideoLAN
- * $Id: audio_output.c,v 1.90 2002/08/07 21:36:56 massiot Exp $
+ * $Id: audio_output.c,v 1.102 2002/09/16 20:46:38 massiot Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
 #include "audio_output.h"
 #include "aout_internal.h"
 
+/*
+ * Instances management (see also input.c:aout_InputNew())
+ */
+
 /*****************************************************************************
  * aout_NewInstance: initialize aout structure
  *****************************************************************************/
@@ -51,15 +55,11 @@ aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
     }
 
     /* Initialize members. */
-    vlc_mutex_init( p_parent, &p_aout->input_lock );
-    vlc_cond_init( p_parent, &p_aout->input_signal );
-    p_aout->i_inputs_active = 0;
-    p_aout->b_change_requested = 0;
-    p_aout->i_nb_inputs = 0;
-
+    vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
     vlc_mutex_init( p_parent, &p_aout->mixer_lock );
-    vlc_cond_init( p_parent, &p_aout->mixer_signal );
-    p_aout->b_mixer_active = 0;
+    vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
+    p_aout->i_nb_inputs = 0;
+    p_aout->mixer.f_multiplier = 1.0;
 
     vlc_object_attach( p_aout, p_parent->p_vlc );
 
@@ -71,16 +71,19 @@ aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
  *****************************************************************************/
 void aout_DeleteInstance( aout_instance_t * p_aout )
 {
-    vlc_mutex_destroy( &p_aout->input_lock );
-    vlc_cond_destroy( &p_aout->input_signal );
+    vlc_mutex_destroy( &p_aout->input_fifos_lock );
     vlc_mutex_destroy( &p_aout->mixer_lock );
-    vlc_cond_destroy( &p_aout->mixer_signal );
+    vlc_mutex_destroy( &p_aout->output_fifo_lock );
 
     /* Free structure. */
-    vlc_object_detach_all( p_aout );
     vlc_object_destroy( p_aout );
 }
 
+
+/*
+ * Buffer management (interface to the decoders)
+ */
+
 /*****************************************************************************
  * aout_BufferNew : ask for a new empty buffer
  *****************************************************************************/
@@ -89,12 +92,14 @@ aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
                                 size_t i_nb_samples )
 {
     aout_buffer_t * p_buffer;
+    mtime_t duration = (1000000 * (mtime_t)i_nb_samples)
+                        / p_input->input.i_rate;
 
     /* This necessarily allocates in the heap. */
-    aout_BufferAlloc( &p_input->input_alloc, (u64)(1000000 * i_nb_samples)
-                                         / p_input->input.i_rate,
-                      NULL, p_buffer );
+    aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
     p_buffer->i_nb_samples = i_nb_samples;
+    p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
+                              / p_input->input.i_frame_length;
 
     if ( p_buffer == NULL )
     {
@@ -123,8 +128,6 @@ void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
 void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
                       aout_buffer_t * p_buffer )
 {
-    vlc_bool_t b_run_mixer = 0;
-
     if ( p_buffer->start_date == 0 )
     {
         msg_Warn( p_aout, "non-dated buffer received" );
@@ -137,32 +140,69 @@ void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
                                     / p_input->input.i_rate;
     }
 
+    /* If the buffer is too early, wait a while. */
+    mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
+
     aout_InputPlay( p_aout, p_input, p_buffer );
 
     /* Run the mixer if it is able to run. */
-    vlc_mutex_lock( &p_aout->mixer_lock );
-    if ( !p_aout->b_mixer_active )
-    {
-        p_aout->b_mixer_active = 1;
-        b_run_mixer = 1;
-    }
-    vlc_mutex_unlock( &p_aout->mixer_lock );
+    aout_MixerRun( p_aout );
+}
+
 
-    if ( b_run_mixer )
+/*
+ * Formats management
+ */
+
+/*****************************************************************************
+ * aout_FormatNbChannels : return the number of channels
+ *****************************************************************************/
+int aout_FormatNbChannels( audio_sample_format_t * p_format )
+{
+    int i_nb;
+
+    switch ( p_format->i_channels & AOUT_CHAN_MASK )
     {
-        aout_MixerRun( p_aout );
-        vlc_mutex_lock( &p_aout->mixer_lock );
-        p_aout->b_mixer_active = 0;
-        vlc_cond_broadcast( &p_aout->mixer_signal );
-        vlc_mutex_unlock( &p_aout->mixer_lock );
+    case AOUT_CHAN_CHANNEL1:
+    case AOUT_CHAN_CHANNEL2:
+    case AOUT_CHAN_MONO:
+        i_nb = 1;
+        break;
+
+    case AOUT_CHAN_CHANNEL:
+    case AOUT_CHAN_STEREO:
+    case AOUT_CHAN_DOLBY:
+        i_nb = 2;
+        break;
+
+    case AOUT_CHAN_3F:
+    case AOUT_CHAN_2F1R:
+        i_nb = 3;
+        break;
+
+    case AOUT_CHAN_3F1R:
+    case AOUT_CHAN_2F2R:
+        i_nb = 4;
+        break;
+
+    case AOUT_CHAN_3F2R:
+        i_nb = 5;
+        break;
+
+    default:
+        i_nb = 0;
     }
+
+    if ( p_format->i_channels & AOUT_CHAN_LFE )
+        return i_nb + 1;
+    else
+        return i_nb;
 }
 
 /*****************************************************************************
- * aout_FormatToBytes : return the number bytes/sample for format
- * (didn't know where else to put it)
+ * aout_FormatPrepare : compute the number of bytes per frame & frame length
  *****************************************************************************/
-int aout_FormatToBytes( audio_sample_format_t * p_format )
+void aout_FormatPrepare( audio_sample_format_t * p_format )
 {
     int i_result;
 
@@ -185,14 +225,196 @@ int aout_FormatToBytes( audio_sample_format_t * p_format )
         i_result = 4;
         break;
 
+    case AOUT_FMT_SPDIF:
     case AOUT_FMT_A52:
-        i_result = 1; /* This is a bit special... sample == byte */
-        break;
+    case AOUT_FMT_DTS:
+        /* For these formats the caller has to indicate the parameters
+         * by hand. */
+        return;
 
     default:
         i_result = 0; /* will segfault much sooner... */
     }
 
-    return i_result * p_format->i_channels;
+    p_format->i_bytes_per_frame = i_result * aout_FormatNbChannels( p_format );
+    p_format->i_frame_length = 1;
+}
+
+
+/*
+ * FIFO management (internal) - please understand that solving race conditions
+ * is _your_ job, ie. in the audio output you should own the mixer lock
+ * before calling any of these functions.
+ */
+
+/*****************************************************************************
+ * aout_FifoInit : initialize the members of a FIFO
+ *****************************************************************************/
+void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
+                    u32 i_rate )
+{
+    p_fifo->p_first = NULL;
+    p_fifo->pp_last = &p_fifo->p_first;
+    aout_DateInit( &p_fifo->end_date, i_rate );
+}
+
+/*****************************************************************************
+ * aout_FifoPush : push a packet into the FIFO
+ *****************************************************************************/
+void aout_FifoPush( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
+                    aout_buffer_t * p_buffer )
+{
+    *p_fifo->pp_last = p_buffer;
+    p_fifo->pp_last = &p_buffer->p_next;
+    *p_fifo->pp_last = NULL;
+    /* Enforce the continuity of the stream. */
+    if ( aout_DateGet( &p_fifo->end_date ) )
+    {
+        p_buffer->start_date = aout_DateGet( &p_fifo->end_date );
+        p_buffer->end_date = aout_DateIncrement( &p_fifo->end_date,
+                                                 p_buffer->i_nb_samples ); 
+    }
+    else
+    {
+        aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
+    }
+}
+
+/*****************************************************************************
+ * aout_FifoSet : set end_date and trash all buffers (because they aren't
+ * properly dated)
+ *****************************************************************************/
+void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
+                   mtime_t date )
+{
+    aout_buffer_t * p_buffer;
+
+    aout_DateSet( &p_fifo->end_date, date );
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
+    {
+        aout_buffer_t * p_next = p_buffer->p_next;
+        aout_BufferFree( p_buffer );
+        p_buffer = p_next;
+    }
+    p_fifo->p_first = NULL;
+    p_fifo->pp_last = &p_fifo->p_first;
+}
+
+/*****************************************************************************
+ * aout_FifoMoveDates : Move forwards or backwards all dates in the FIFO
+ *****************************************************************************/
+void aout_FifoMoveDates( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
+                         mtime_t difference )
+{
+    aout_buffer_t * p_buffer;
+
+    aout_DateMove( &p_fifo->end_date, difference );
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
+    {
+        p_buffer->start_date += difference;
+        p_buffer->end_date += difference;
+        p_buffer = p_buffer->p_next;
+    }
+}
+
+/*****************************************************************************
+ * aout_FifoNextStart : return the current end_date
+ *****************************************************************************/
+mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
+{
+    return aout_DateGet( &p_fifo->end_date );
+}
+
+/*****************************************************************************
+ * aout_FifoPop : get the next buffer out of the FIFO
+ *****************************************************************************/
+aout_buffer_t * aout_FifoPop( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
+{
+    aout_buffer_t * p_buffer;
+    p_buffer = p_fifo->p_first;
+    if ( p_buffer == NULL ) return NULL;
+    p_fifo->p_first = p_buffer->p_next;
+    if ( p_fifo->p_first == NULL )
+    {
+        p_fifo->pp_last = &p_fifo->p_first;
+    }
+
+    return p_buffer;
+}
+
+/*****************************************************************************
+ * aout_FifoDestroy : destroy a FIFO and its buffers
+ *****************************************************************************/
+void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
+{
+    aout_buffer_t * p_buffer;
+
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
+    {
+        aout_buffer_t * p_next = p_buffer->p_next;
+        aout_BufferFree( p_buffer );
+        p_buffer = p_next;
+    }
+}
+
+
+/*
+ * Date management (internal and external)
+ */
+
+/*****************************************************************************
+ * aout_DateInit : set the divider of an audio_date_t
+ *****************************************************************************/
+void aout_DateInit( audio_date_t * p_date, u32 i_divider )
+{
+    p_date->date = 0;
+    p_date->i_divider = i_divider;
+    p_date->i_remainder = 0;
+}
+
+/*****************************************************************************
+ * aout_DateSet : set the date of an audio_date_t
+ *****************************************************************************/
+void aout_DateSet( audio_date_t * p_date, mtime_t new_date )
+{
+    p_date->date = new_date;
+    p_date->i_remainder = 0;
+}
+
+/*****************************************************************************
+ * aout_DateMove : move forwards or backwards the date of an audio_date_t
+ *****************************************************************************/
+void aout_DateMove( audio_date_t * p_date, mtime_t difference )
+{
+    p_date->date += difference;
+}
+
+/*****************************************************************************
+ * aout_DateGet : get the date of an audio_date_t
+ *****************************************************************************/
+mtime_t aout_DateGet( const audio_date_t * p_date )
+{
+    return p_date->date;
+}
+
+/*****************************************************************************
+ * aout_DateIncrement : increment the date and return the result, taking
+ * into account rounding errors
+ *****************************************************************************/
+mtime_t aout_DateIncrement( audio_date_t * p_date, u32 i_nb_samples )
+{
+    mtime_t i_dividend = (mtime_t)i_nb_samples * 1000000;
+    p_date->date += i_dividend / p_date->i_divider;
+    p_date->i_remainder += i_dividend % p_date->i_divider;
+    if ( p_date->i_remainder >= p_date->i_divider )
+    {
+        /* This is Bresenham algorithm. */
+        p_date->date++;
+        p_date->i_remainder -= p_date->i_divider;
+    }
+    return p_date->date;
 }