]> 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 de60d0348bba8bd9054f7eee3920383b2aa97534..d070fc984ea97472f7da8a0b14a62df94542dd6f 100644 (file)
@@ -1,10 +1,10 @@
 /*****************************************************************************
- * audio_output.c : audio output thread
+ * audio_output.c : audio output instance miscellaneous functions
  *****************************************************************************
- * Copyright (C) 1999, 2000, 2001 VideoLAN
- * $Id: audio_output.c,v 1.57 2001/04/28 03:36:25 sam Exp $
+ * Copyright (C) 2002 VideoLAN
+ * $Id: audio_output.c,v 1.102 2002/09/16 20:46:38 massiot Exp $
  *
- * Authors: Michel Kaempf <maxx@via.ecp.fr>
+ * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 
-/* TODO:
- *
- * - Passer un certain nombre de "fonctions" (genre add_samples) en macro ou
- *   inline
- * - Faire les optimisations dans les fonctions threads :
- *   = Stocker les "petits calculs" dans des variables au lieu de les refaire
- *     à chaque boucle
- *   = Utiliser des tables pour les gros calculs
- * - Faire une structure différente pour intf/adec fifo
- *
- */
-
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include "defs.h"
-
-#include <unistd.h>                                              /* getpid() */
-#ifdef WIN32                   /* getpid() for win32 is located in process.h */
-#include <process.h>
-#endif
-
-#include <stdio.h>                                           /* "intf_msg.h" */
 #include <stdlib.h>                            /* calloc(), malloc(), free() */
+#include <string.h>
 
-#include "config.h"
-#include "common.h"
-#include "threads.h"
-#include "mtime.h"                             /* mtime_t, mdate(), msleep() */
-#include "modules.h"
+#include <vlc/vlc.h>
 
-#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
+#ifdef HAVE_ALLOCA_H
+#   include <alloca.h>
+#endif
 
 #include "audio_output.h"
-#include "aout_common.h"
-
-#include "main.h"
+#include "aout_internal.h"
 
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int aout_SpawnThread( aout_thread_t * p_aout );
+/*
+ * Instances management (see also input.c:aout_InputNew())
+ */
 
 /*****************************************************************************
- * aout_CreateThread: initialize audio thread
+ * aout_NewInstance: initialize aout structure
  *****************************************************************************/
-aout_thread_t *aout_CreateThread( int *pi_status )
+aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
 {
-    aout_thread_t * p_aout;                             /* thread descriptor */
-#if 0
-    int             i_status;                               /* thread status */
-#endif
+    aout_instance_t * p_aout;
 
-    /* Allocate descriptor */
-    p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
+    /* Allocate descriptor. */
+    p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
     if( p_aout == NULL )
     {
-        return( NULL );
+        return NULL;
     }
 
-    /* Choose the best module */
-    p_aout->p_module = module_Need( p_main->p_bank,
-                                    MODULE_CAPABILITY_AOUT, NULL );
+    /* Initialize members. */
+    vlc_mutex_init( p_parent, &p_aout->input_fifos_lock );
+    vlc_mutex_init( p_parent, &p_aout->mixer_lock );
+    vlc_mutex_init( p_parent, &p_aout->output_fifo_lock );
+    p_aout->i_nb_inputs = 0;
+    p_aout->mixer.f_multiplier = 1.0;
 
-    if( p_aout->p_module == NULL )
-    {
-        intf_ErrMsg( "aout error: no suitable aout module" );
-        free( p_aout );
-        return( NULL );
-    }
+    vlc_object_attach( p_aout, p_parent->p_vlc );
+
+    return p_aout;
+}
 
-#define aout_functions p_aout->p_module->p_functions->aout.functions.aout
-    p_aout->pf_open       = aout_functions.pf_open;
-    p_aout->pf_setformat  = aout_functions.pf_setformat;
-    p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
-    p_aout->pf_play       = aout_functions.pf_play;
-    p_aout->pf_close      = aout_functions.pf_close;
-#undef aout_functions
-
-    /*
-     * Initialize audio device
-     */
-    if ( p_aout->pf_open( p_aout ) )
+/*****************************************************************************
+ * aout_DeleteInstance: destroy aout structure
+ *****************************************************************************/
+void aout_DeleteInstance( aout_instance_t * p_aout )
+{
+    vlc_mutex_destroy( &p_aout->input_fifos_lock );
+    vlc_mutex_destroy( &p_aout->mixer_lock );
+    vlc_mutex_destroy( &p_aout->output_fifo_lock );
+
+    /* Free structure. */
+    vlc_object_destroy( p_aout );
+}
+
+
+/*
+ * Buffer management (interface to the decoders)
+ */
+
+/*****************************************************************************
+ * aout_BufferNew : ask for a new empty buffer
+ *****************************************************************************/
+aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
+                                aout_input_t * p_input,
+                                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, 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 )
     {
-        module_Unneed( p_main->p_bank, p_aout->p_module );
-        free( p_aout );
-        return( NULL );
+        msg_Err( p_aout, "NULL buffer !" );
     }
-
-    if( p_aout->l_rate == 0 )
+    else
     {
-        intf_ErrMsg( "aout error: null sample rate" );
-        p_aout->pf_close( p_aout );
-        module_Unneed( p_main->p_bank, p_aout->p_module );
-        free( p_aout );
-        return( NULL );
+        p_buffer->start_date = p_buffer->end_date = 0;
     }
 
-    /* FIXME: only works for i_channels == 1 or 2 ?? */
-    p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
+    return p_buffer;
+}
 
-    if ( p_aout->pf_setformat( p_aout ) )
+/*****************************************************************************
+ * aout_BufferDelete : destroy an undecoded buffer
+ *****************************************************************************/
+void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
+                        aout_buffer_t * p_buffer )
+{
+    aout_BufferFree( p_buffer );
+}
+
+/*****************************************************************************
+ * aout_BufferPlay : filter & mix the decoded buffer
+ *****************************************************************************/
+void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
+                      aout_buffer_t * p_buffer )
+{
+    if ( p_buffer->start_date == 0 )
     {
-        p_aout->pf_close( p_aout );
-        module_Unneed( p_main->p_bank, p_aout->p_module );
-        free( p_aout );
-        return( NULL );
+        msg_Warn( p_aout, "non-dated buffer received" );
+        aout_BufferFree( p_buffer );
     }
-
-    /* Initialize the volume level */
-    p_aout->vol = VOLUME_DEFAULT;
-    
-    /* FIXME: maybe it would be cleaner to change SpawnThread prototype
-     * see vout to handle status correctly ?? however, it is not critical since
-     * this thread is only called in main and all calls are blocking */
-    if( aout_SpawnThread( p_aout ) )
+    else
     {
-        p_aout->pf_close( p_aout );
-        module_Unneed( p_main->p_bank, p_aout->p_module );
-        free( p_aout );
-        return( NULL );
+        p_buffer->end_date = p_buffer->start_date
+                                + (mtime_t)(p_buffer->i_nb_samples * 1000000)
+                                    / p_input->input.i_rate;
     }
 
-    return( p_aout );
+    /* 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. */
+    aout_MixerRun( p_aout );
 }
 
+
+/*
+ * Formats management
+ */
+
 /*****************************************************************************
- * aout_SpawnThread
+ * aout_FormatNbChannels : return the number of channels
  *****************************************************************************/
-static int aout_SpawnThread( aout_thread_t * p_aout )
+int aout_FormatNbChannels( audio_sample_format_t * p_format )
 {
-    int             i_fifo;
-    long            l_bytes;
-    void *          aout_thread = NULL;
-
-    /* We want the audio output thread to live */
-    p_aout->b_die = 0;
-    p_aout->b_active = 1;
-
-    /* Initialize the fifos lock */
-    vlc_mutex_init( &p_aout->fifos_lock );
-    /* Initialize audio fifos : set all fifos as empty and initialize locks */
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
+    int i_nb;
+
+    switch ( p_format->i_channels & AOUT_CHAN_MASK )
     {
-        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
-        vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
-        vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
+    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;
     }
 
-    /* Compute the size (in audio units) of the audio output buffer. Although
-     * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
-     * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
-    p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
-    p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
+    if ( p_format->i_channels & AOUT_CHAN_LFE )
+        return i_nb + 1;
+    else
+        return i_nb;
+}
+
+/*****************************************************************************
+ * aout_FormatPrepare : compute the number of bytes per frame & frame length
+ *****************************************************************************/
+void aout_FormatPrepare( audio_sample_format_t * p_format )
+{
+    int i_result;
 
-    /* Make aout_thread point to the right thread function, and compute the
-     * byte size of the audio output buffer */
-    switch ( p_aout->i_channels )
+    switch ( p_format->i_format )
     {
-    /* Audio output is mono */
-    case 1:
-        switch ( p_aout->i_format )
-        {
-        case AOUT_FMT_U8:
-            intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
-            l_bytes = 1 * sizeof(u8) * p_aout->l_units;
-            aout_thread = (void *)aout_U8MonoThread;
-            break;
-
-        case AOUT_FMT_S8:
-            intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
-            l_bytes = 1 * sizeof(s8) * p_aout->l_units;
-            aout_thread = (void *)aout_S8MonoThread;
-            break;
-
-        case AOUT_FMT_U16_LE:
-        case AOUT_FMT_U16_BE:
-            intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
-            l_bytes = 1 * sizeof(u16) * p_aout->l_units;
-            aout_thread = (void *)aout_U16MonoThread;
-            break;
-
-        case AOUT_FMT_S16_LE:
-        case AOUT_FMT_S16_BE:
-            intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
-            l_bytes = 1 * sizeof(s16) * p_aout->l_units;
-            aout_thread = (void *)aout_S16MonoThread;
-            break;
-
-                default:
-            intf_ErrMsg( "aout error: unknown audio output format (%i)",
-                         p_aout->i_format );
-            return( -1 );
-        }
+    case AOUT_FMT_U8:
+    case AOUT_FMT_S8:
+        i_result = 1;
+        break;
+
+    case AOUT_FMT_U16_LE:
+    case AOUT_FMT_U16_BE:
+    case AOUT_FMT_S16_LE:
+    case AOUT_FMT_S16_BE:
+        i_result = 2;
         break;
 
-    /* Audio output is stereo */
-    case 2:
-        switch ( p_aout->i_format )
-        {
-        case AOUT_FMT_U8:
-            intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
-            l_bytes = 2 * sizeof(u8) * p_aout->l_units;
-            aout_thread = (void *)aout_U8StereoThread;
-            break;
-
-        case AOUT_FMT_S8:
-            intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
-            l_bytes = 2 * sizeof(s8) * p_aout->l_units;
-            aout_thread = (void *)aout_S8StereoThread;
-            break;
-
-        case AOUT_FMT_U16_LE:
-        case AOUT_FMT_U16_BE:
-            intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
-            l_bytes = 2 * sizeof(u16) * p_aout->l_units;
-            aout_thread = (void *)aout_U16StereoThread;
-            break;
-
-        case AOUT_FMT_S16_LE:
-        case AOUT_FMT_S16_BE:
-            intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
-            l_bytes = 2 * sizeof(s16) * p_aout->l_units;
-            aout_thread = (void *)aout_S16StereoThread;
-            break;
-
-        default:
-            intf_ErrMsg( "aout error: unknown audio output format %i",
-                         p_aout->i_format );
-            return( -1 );
-        }
+    case AOUT_FMT_FLOAT32:
+    case AOUT_FMT_FIXED32:
+        i_result = 4;
         break;
 
+    case AOUT_FMT_SPDIF:
+    case AOUT_FMT_A52:
+    case AOUT_FMT_DTS:
+        /* For these formats the caller has to indicate the parameters
+         * by hand. */
+        return;
+
     default:
-        intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
-                     p_aout->i_channels );
-        return( -1 );
+        i_result = 0; /* will segfault much sooner... */
     }
 
-    /* Allocate the memory needed by the audio output buffers, and set to zero
-     * the s32 buffer's memory */
-    p_aout->buffer = malloc( l_bytes );
-    if ( p_aout->buffer == NULL )
+    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 ) )
     {
-        intf_ErrMsg( "aout error: cannot create output buffer" );
-        return( -1 );
+        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 ); 
     }
-
-    p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
-                                        sizeof(s32) << ( p_aout->b_stereo ) );
-    if ( p_aout->s32_buffer == NULL )
+    else
     {
-        intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
-        free( p_aout->buffer );
-        return( -1 );
+        aout_DateSet( &p_fifo->end_date, p_buffer->end_date );
     }
+}
 
-    /* Rough estimate of the playing date */
-    p_aout->date = mdate();
+/*****************************************************************************
+ * 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;
 
-    /* Launch the thread */
-    if ( vlc_thread_create( &p_aout->thread_id, "audio output",
-                            (vlc_thread_func_t)aout_thread, p_aout ) )
+    aout_DateSet( &p_fifo->end_date, date );
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
     {
-        intf_ErrMsg( "aout error: cannot spawn audio output thread" );
-        free( p_aout->buffer );
-        free( p_aout->s32_buffer );
-        return( -1 );
+        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;
 
-    intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
-    return( 0 );
+    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_DestroyThread
+ * aout_FifoNextStart : return the current end_date
  *****************************************************************************/
-void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
+mtime_t aout_FifoNextStart( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
 {
+    return aout_DateGet( &p_fifo->end_date );
+}
 
-    int i_fifo;
-    
-    /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
+/*****************************************************************************
+ * 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;
+    }
 
-    /* Ask thread to kill itself and wait until it's done */
-    p_aout->b_die = 1;
-    vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
+    return p_buffer;
+}
 
-    /* Free the allocated memory */
-    free( p_aout->buffer );
-    free( p_aout->s32_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;
 
-    /* Destroy the condition and mutex locks */
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
     {
-        vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
-        vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
+        aout_buffer_t * p_next = p_buffer->p_next;
+        aout_BufferFree( p_buffer );
+        p_buffer = p_next;
     }
-    vlc_mutex_destroy( &p_aout->fifos_lock );
-    
-    /* Free the plugin */
-    p_aout->pf_close( p_aout );
+}
+
+
+/*
+ * 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;
+}
 
-    /* Release the aout module */
-    module_Unneed( p_main->p_bank, p_aout->p_module );
+/*****************************************************************************
+ * aout_DateGet : get the date of an audio_date_t
+ *****************************************************************************/
+mtime_t aout_DateGet( const audio_date_t * p_date )
+{
+    return p_date->date;
+}
 
-    /* Free structure */
-    free( p_aout );
+/*****************************************************************************
+ * 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;
 }