]> 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 d5d89561a9ada1ee6f669606bb9fe3349e0d6fe8..d070fc984ea97472f7da8a0b14a62df94542dd6f 100644 (file)
 /*****************************************************************************
- * audio_output.c : audio output thread
+ * audio_output.c : audio output instance miscellaneous functions
  *****************************************************************************
- * Copyright (C) 1999, 2000 VideoLAN
+ * Copyright (C) 2002 VideoLAN
+ * $Id: audio_output.c,v 1.102 2002/09/16 20:46:38 massiot Exp $
  *
- * Authors:
+ * 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
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- *
+ * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * 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() */
-
-#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 "plugins.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 "main.h"
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-
-static int aout_SpawnThread( aout_thread_t * p_aout );
+#include "aout_internal.h"
 
-/* Creating as much aout_Thread functions as configurations is one solution,
- * examining the different cases in the Thread loop of an unique function is
- * another. I chose the first solution. */
-void aout_Thread_S8_Mono        ( aout_thread_t * p_aout );
-void aout_Thread_U8_Mono        ( aout_thread_t * p_aout );
-void aout_Thread_S16_Mono       ( aout_thread_t * p_aout );
-void aout_Thread_U16_Mono       ( aout_thread_t * p_aout );
-void aout_Thread_S8_Stereo      ( aout_thread_t * p_aout );
-void aout_Thread_U8_Stereo      ( aout_thread_t * p_aout );
-void aout_Thread_S16_Stereo     ( aout_thread_t * p_aout );
-void aout_Thread_U16_Stereo     ( aout_thread_t * p_aout );
-
-static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator );
-static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date );
+/*
+ * 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 */
-    char * psz_method;
-#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 );
-    }
-
-    /* Request an interface plugin */
-    psz_method = main_GetPszVariable( AOUT_METHOD_VAR, AOUT_DEFAULT_METHOD );
-
-    if( RequestPlugin( &p_aout->aout_plugin, "aout", psz_method ) )
-    {
-        intf_ErrMsg( "error: could not open audio plugin aout_%s.so\n", psz_method );
-        free( p_aout );
-        return( NULL );
+        return NULL;
     }
 
-    /* Get plugins */
-    p_aout->p_sys_open =         GetPluginFunction( p_aout->aout_plugin, "aout_SysOpen" );
-    p_aout->p_sys_reset =        GetPluginFunction( p_aout->aout_plugin, "aout_SysReset" );
-    p_aout->p_sys_setformat =    GetPluginFunction( p_aout->aout_plugin, "aout_SysSetFormat" );
-    p_aout->p_sys_setchannels =  GetPluginFunction( p_aout->aout_plugin, "aout_SysSetChannels" );
-    p_aout->p_sys_setrate =      GetPluginFunction( p_aout->aout_plugin, "aout_SysSetRate" );
-    p_aout->p_sys_getbufinfo =   GetPluginFunction( p_aout->aout_plugin, "aout_SysGetBufInfo" );
-    p_aout->p_sys_playsamples =  GetPluginFunction( p_aout->aout_plugin, "aout_SysPlaySamples" );
-    p_aout->p_sys_close =        GetPluginFunction( p_aout->aout_plugin, "aout_SysClose" );
-
-    /*
-     * Initialize audio device
-     */
-    if ( p_aout->p_sys_open( p_aout ) )
-    {
-        TrashPlugin( p_aout->aout_plugin );
-        free( p_aout );
-        return( 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;
 
-    p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0; /* FIXME: only works
-                                                   for i_channels == 1 or 2 ??*/
+    vlc_object_attach( p_aout, p_parent->p_vlc );
 
-    if ( p_aout->p_sys_reset( p_aout ) )
-    {
-        p_aout->p_sys_close( p_aout );
-        TrashPlugin( p_aout->aout_plugin );
-        free( p_aout );
-        return( NULL );
-    }
-    if ( p_aout->p_sys_setformat( p_aout ) )
-    {
-        p_aout->p_sys_close( p_aout );
-        TrashPlugin( p_aout->aout_plugin );
-        free( p_aout );
-        return( NULL );
-    }
-    if ( p_aout->p_sys_setchannels( p_aout ) )
-    {
-        p_aout->p_sys_close( p_aout );
-        TrashPlugin( p_aout->aout_plugin );
-        free( p_aout );
-        return( NULL );
-    }
-    if ( p_aout->p_sys_setrate( p_aout ) )
-    {
-        p_aout->p_sys_close( p_aout );
-        TrashPlugin( p_aout->aout_plugin );
-        free( p_aout );
-        return( NULL );
-    }
-
-    /* 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 ) )
-    {
-        p_aout->p_sys_close( p_aout );
-        TrashPlugin( p_aout->aout_plugin );
-        free( p_aout );
-        return( NULL );
-    }
-
-    return( p_aout );
+    return p_aout;
 }
 
 /*****************************************************************************
- * aout_SpawnThread
+ * aout_DeleteInstance: destroy aout structure
  *****************************************************************************/
-static int aout_SpawnThread( aout_thread_t * p_aout )
+void aout_DeleteInstance( aout_instance_t * p_aout )
 {
-    int             i_fifo;
-    long            l_bytes;
-    void *          aout_thread = NULL;
-
-    intf_DbgMsg("aout debug: spawning audio output thread (%p)\n", p_aout);
-
-    /* We want the audio output thread to live */
-    p_aout->b_die = 0;
-
-    /* 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++ )
-    {
-        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 );
-    }
-
-    /* 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 );
-
-    /* Make aout_thread point to the right thread function, and compute the
-     * byte size of the audio output buffer */
-    switch ( p_aout->i_channels )
-    {
-        /* Audio output is mono */
-        case 1:
-            switch ( p_aout->i_format )
-            {
-                case AOUT_FMT_U8:
-                    l_bytes = 1 * sizeof(u8) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_U8_Mono;
-                    break;
-
-                case AOUT_FMT_S8:
-                    l_bytes = 1 * sizeof(s8) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_S8_Mono;
-                    break;
-
-                case AOUT_FMT_U16_LE:
-                case AOUT_FMT_U16_BE:
-                    l_bytes = 1 * sizeof(u16) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_U16_Mono;
-                    break;
-
-                case AOUT_FMT_S16_LE:
-                case AOUT_FMT_S16_BE:
-                    l_bytes = 1 * sizeof(s16) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_S16_Mono;
-                    break;
-
-                default:
-                    intf_ErrMsg( "aout error: unknown audio output format (%i)\n",
-                                 p_aout->i_format );
-                    return( -1 );
-            }
-            break;
-
-        /* Audio output is stereo */
-        case 2:
-            switch ( p_aout->i_format )
-            {
-                case AOUT_FMT_U8:
-                    l_bytes = 2 * sizeof(u8) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_U8_Stereo;
-                    break;
+    vlc_mutex_destroy( &p_aout->input_fifos_lock );
+    vlc_mutex_destroy( &p_aout->mixer_lock );
+    vlc_mutex_destroy( &p_aout->output_fifo_lock );
 
-                case AOUT_FMT_S8:
-                    l_bytes = 2 * sizeof(s8) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_S8_Stereo;
-                    break;
+    /* Free structure. */
+    vlc_object_destroy( p_aout );
+}
 
-                case AOUT_FMT_U16_LE:
-                case AOUT_FMT_U16_BE:
-                    l_bytes = 2 * sizeof(u16) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_U16_Stereo;
-                    break;
 
-                case AOUT_FMT_S16_LE:
-                case AOUT_FMT_S16_BE:
-                    l_bytes = 2 * sizeof(s16) * p_aout->l_units;
-                    aout_thread = (void *)aout_Thread_S16_Stereo;
-                    break;
+/*
+ * Buffer management (interface to the decoders)
+ */
 
-                default:
-                    intf_ErrMsg("aout error: unknown audio output format (%i)\n",
-                        p_aout->i_format);
-                    return( -1 );
-            }
-            break;
+/*****************************************************************************
+ * 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;
 
-        default:
-            intf_ErrMsg("aout error: unknown number of audio channels (%i)\n",
-                p_aout->i_channels );
-            return( -1 );
-    }
+    /* 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;
 
-    /* Allocate the memory needed by the audio output buffers, and set to zero
-     * the s32 buffer's memory */
-    if ( (p_aout->buffer = malloc(l_bytes)) == NULL )
+    if ( p_buffer == NULL )
     {
-        intf_ErrMsg("aout error: not enough memory to create the output buffer\n");
-        return( -1 );
+        msg_Err( p_aout, "NULL buffer !" );
     }
-    if ( (p_aout->s32_buffer = (s32 *)calloc(p_aout->l_units, sizeof(s32) << ( p_aout->b_stereo))) == NULL )
+    else
     {
-        intf_ErrMsg("aout error: not enough memory to create the s32 output buffer\n");
-        free( p_aout->buffer );
-        return( -1 );
+        p_buffer->start_date = p_buffer->end_date = 0;
     }
 
-    /* Before launching the thread, we try to predict the date of the first
-     * audio unit in the first output buffer */
-    p_aout->date = mdate() - 1000000;
-
-    /* Launch the thread */
-    if ( vlc_thread_create( &p_aout->thread_id, "audio output", (vlc_thread_func_t)aout_thread, p_aout ) )
-    {
-        intf_ErrMsg("aout error: can't spawn audio output thread (%p)\n", p_aout);
-        free( p_aout->buffer );
-        free( p_aout->s32_buffer );
-        return( -1 );
-    }
-
-    intf_DbgMsg("aout debug: audio output thread (%p) spawned\n", p_aout);
-    return( 0 );
+    return p_buffer;
 }
 
 /*****************************************************************************
- * aout_DestroyThread
+ * aout_BufferDelete : destroy an undecoded buffer
  *****************************************************************************/
-void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
+void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
+                        aout_buffer_t * p_buffer )
 {
-    /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
-
-    intf_DbgMsg("aout debug: requesting termination of audio output thread (%p)\n", p_aout);
-
-    /* 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 */
-
-    /* Free the allocated memory */
-    free( p_aout->buffer );
-    free( p_aout->s32_buffer );
-
-    /* Free the structure */
-    p_aout->p_sys_close( p_aout );
-    intf_DbgMsg("aout debug: audio device (%s) closed\n", p_aout->psz_device);
-
-    /* Close plugin */
-    TrashPlugin( p_aout->aout_plugin );
-
-    /* Free structure */
-    free( p_aout );
+    aout_BufferFree( p_buffer );
 }
 
 /*****************************************************************************
- * aout_CreateFifo
+ * aout_BufferPlay : filter & mix the decoded buffer
  *****************************************************************************/
-aout_fifo_t * aout_CreateFifo( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
+void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
+                      aout_buffer_t * p_buffer )
 {
-    int i_fifo;
-
-    /* Take the fifos lock */
-    vlc_mutex_lock( &p_aout->fifos_lock );
-
-    /* Looking for a free fifo structure */
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
+    if ( p_buffer->start_date == 0 )
     {
-        if ( p_aout->fifo[i_fifo].i_type == AOUT_EMPTY_FIFO)
-        {
-            break;
-        }
+        msg_Warn( p_aout, "non-dated buffer received" );
+        aout_BufferFree( p_buffer );
     }
-    if ( i_fifo == AOUT_MAX_FIFOS )
+    else
     {
-        intf_ErrMsg("aout error: no empty fifo available\n");
-        vlc_mutex_unlock( &p_aout->fifos_lock );
-        return( NULL );
+        p_buffer->end_date = p_buffer->start_date
+                                + (mtime_t)(p_buffer->i_nb_samples * 1000000)
+                                    / p_input->input.i_rate;
     }
 
-    /* Initialize the new fifo structure */
-    switch ( p_aout->fifo[i_fifo].i_type = p_fifo->i_type )
-    {
-        case AOUT_INTF_MONO_FIFO:
-        case AOUT_INTF_STEREO_FIFO:
-            p_aout->fifo[i_fifo].b_die = 0;
-
-            p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
-            p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
-            p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
-
-            p_aout->fifo[i_fifo].buffer = p_fifo->buffer;
-
-            p_aout->fifo[i_fifo].l_unit = 0;
-            InitializeIncrement( &p_aout->fifo[i_fifo].unit_increment, p_fifo->l_rate, p_aout->l_rate );
-            p_aout->fifo[i_fifo].l_units = p_fifo->l_units;
-            break;
-
-        case AOUT_ADEC_MONO_FIFO:
-        case AOUT_ADEC_STEREO_FIFO:
-            p_aout->fifo[i_fifo].b_die = 0;
+    /* If the buffer is too early, wait a while. */
+    mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
 
-            p_aout->fifo[i_fifo].i_channels = p_fifo->i_channels;
-            p_aout->fifo[i_fifo].b_stereo = p_fifo->b_stereo;
-            p_aout->fifo[i_fifo].l_rate = p_fifo->l_rate;
+    aout_InputPlay( p_aout, p_input, p_buffer );
 
-            p_aout->fifo[i_fifo].l_frame_size = p_fifo->l_frame_size;
-            /* Allocate the memory needed to store the audio frames. As the
-             * fifo is a rotative fifo, we must be able to find out whether the
-             * fifo is full or empty, that's why we must in fact allocate memory
-             * for (AOUT_FIFO_SIZE+1) audio frames. */
-            if ( (p_aout->fifo[i_fifo].buffer = malloc( sizeof(s16)*(AOUT_FIFO_SIZE+1)*p_fifo->l_frame_size )) == NULL )
-            {
-                intf_ErrMsg("aout error: not enough memory to create the frames buffer\n");
-                p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
-                vlc_mutex_unlock( &p_aout->fifos_lock );
-                return( NULL );
-            }
+    /* Run the mixer if it is able to run. */
+    aout_MixerRun( p_aout );
+}
 
-            /* Allocate the memory needed to store the dates of the frames */
-            if ( (p_aout->fifo[i_fifo].date = (mtime_t *)malloc( sizeof(mtime_t)*(AOUT_FIFO_SIZE+1) )) == NULL )
-            {
-                intf_ErrMsg("aout error: not enough memory to create the dates buffer\n");
-                free( p_aout->fifo[i_fifo].buffer );
-                p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
-                vlc_mutex_unlock( &p_aout->fifos_lock );
-                return( NULL );
-            }
 
-            /* Set the fifo's buffer as empty (the first frame that is to be
-             * played is also the first frame that is not to be played) */
-            p_aout->fifo[i_fifo].l_start_frame = 0;
-            /* p_aout->fifo[i_fifo].l_next_frame = 0; */
-            p_aout->fifo[i_fifo].l_end_frame = 0;
+/*
+ * Formats management
+ */
 
-            /* Waiting for the audio decoder to compute enough frames to work
-             * out the fifo's current rate (as soon as the decoder has decoded
-             * enough frames, the members of the fifo structure that are not
-             * initialized now will be calculated) */
-            p_aout->fifo[i_fifo].b_start_frame = 0;
-            p_aout->fifo[i_fifo].b_next_frame = 0;
-            break;
+/*****************************************************************************
+ * aout_FormatNbChannels : return the number of channels
+ *****************************************************************************/
+int aout_FormatNbChannels( audio_sample_format_t * p_format )
+{
+    int i_nb;
 
-        default:
-            intf_ErrMsg("aout error: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
-            p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
-            vlc_mutex_unlock( &p_aout->fifos_lock );
-            return( NULL );
+    switch ( p_format->i_channels & AOUT_CHAN_MASK )
+    {
+    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;
     }
 
-    /* Release the fifos lock */
-    vlc_mutex_unlock( &p_aout->fifos_lock );
-
-    /* Return the pointer to the fifo structure */
-    intf_DbgMsg("aout debug: audio output fifo (%p) allocated\n", &p_aout->fifo[i_fifo]);
-    return( &p_aout->fifo[i_fifo] );
+    if ( p_format->i_channels & AOUT_CHAN_LFE )
+        return i_nb + 1;
+    else
+        return i_nb;
 }
 
 /*****************************************************************************
- * aout_DestroyFifo
+ * aout_FormatPrepare : compute the number of bytes per frame & frame length
  *****************************************************************************/
-void aout_DestroyFifo( aout_fifo_t * p_fifo )
+void aout_FormatPrepare( audio_sample_format_t * p_format )
 {
-    intf_DbgMsg("aout debug: requesting destruction of audio output fifo (%p)\n", p_fifo);
-    p_fifo->b_die = 1;
-}
-
-/* Here are the local macros */
+    int i_result;
 
-#define UPDATE_INCREMENT( increment, integer ) \
-    if ( ((increment).l_remainder += (increment).l_euclidean_remainder) >= 0 )\
-    { \
-        (integer) += (increment).l_euclidean_integer + 1; \
-        (increment).l_remainder -= (increment).l_euclidean_denominator; \
-    } \
-    else \
-    { \
-        (integer) += (increment).l_euclidean_integer; \
+    switch ( p_format->i_format )
+    {
+    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;
+
+    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:
+        i_result = 0; /* will segfault much sooner... */
     }
 
-/* Following functions are local */
+    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.
+ */
 
 /*****************************************************************************
- * InitializeIncrement
+ * aout_FifoInit : initialize the members of a FIFO
  *****************************************************************************/
-static __inline__ void InitializeIncrement( aout_increment_t * p_increment, long l_numerator, long l_denominator )
+void aout_FifoInit( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
+                    u32 i_rate )
 {
-    p_increment->l_remainder = -l_denominator;
+    p_fifo->p_first = NULL;
+    p_fifo->pp_last = &p_fifo->p_first;
+    aout_DateInit( &p_fifo->end_date, i_rate );
+}
 
-    p_increment->l_euclidean_integer = 0;
-    while ( l_numerator >= l_denominator )
+/*****************************************************************************
+ * 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_increment->l_euclidean_integer++;
-        l_numerator -= l_denominator;
+        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 );
     }
-
-    p_increment->l_euclidean_remainder = l_numerator;
-
-    p_increment->l_euclidean_denominator = l_denominator;
 }
 
 /*****************************************************************************
- * NextFrame
+ * aout_FifoSet : set end_date and trash all buffers (because they aren't
+ * properly dated)
  *****************************************************************************/
-static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo, mtime_t aout_date )
+void aout_FifoSet( aout_instance_t * p_aout, aout_fifo_t * p_fifo,
+                   mtime_t date )
 {
-    long l_units, l_rate;
-
-    /* We take the lock */
-    vlc_mutex_lock( &p_fifo->data_lock );
+    aout_buffer_t * p_buffer;
 
-    /* Are we looking for a dated start frame ? */
-    if ( !p_fifo->b_start_frame )
+    aout_DateSet( &p_fifo->end_date, date );
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
     {
-        while ( p_fifo->l_start_frame != p_fifo->l_end_frame )
-        {
-            if ( p_fifo->date[p_fifo->l_start_frame] != LAST_MDATE )
-            {
-                p_fifo->b_start_frame = 1;
-                p_fifo->l_next_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
-                p_fifo->l_unit = p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->b_stereo));
-                break;
-            }
-            p_fifo->l_start_frame = (p_fifo->l_start_frame + 1) & AOUT_FIFO_SIZE;
-        }
-
-        if ( p_fifo->l_start_frame == p_fifo->l_end_frame )
-        {
-            vlc_mutex_unlock( &p_fifo->data_lock );
-            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;
+}
 
-    /* We are looking for the next dated frame */
-    /* FIXME : is the output fifo full ?? */
-    while ( !p_fifo->b_next_frame )
-    {
-        while ( p_fifo->l_next_frame != p_fifo->l_end_frame )
-        {
-            if ( p_fifo->date[p_fifo->l_next_frame] != LAST_MDATE )
-            {
-                p_fifo->b_next_frame = 1;
-                break;
-            }
-            p_fifo->l_next_frame = (p_fifo->l_next_frame + 1) & AOUT_FIFO_SIZE;
-        }
+/*****************************************************************************
+ * 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;
 
-        while ( p_fifo->l_next_frame == p_fifo->l_end_frame )
-        {
-            vlc_cond_wait( &p_fifo->data_wait, &p_fifo->data_lock );
-            if ( p_fifo->b_die )
-            {
-                vlc_mutex_unlock( &p_fifo->data_lock );
-                return( -1 );
-            }
-        }
+    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;
     }
-
-    l_units = ((p_fifo->l_next_frame - p_fifo->l_start_frame) & AOUT_FIFO_SIZE) * (p_fifo->l_frame_size >> (p_fifo->b_stereo));
-
-    l_rate = p_fifo->l_rate + ((aout_date - p_fifo->date[p_fifo->l_start_frame]) / 256);
-//    fprintf( stderr, "aout debug: %lli (%li);\n", aout_date - p_fifo->date[p_fifo->l_start_frame], l_rate );
-
-    InitializeIncrement( &p_fifo->unit_increment, l_rate, p_aout->l_rate );
-
-    p_fifo->l_units = (((l_units - (p_fifo->l_unit -
-        (p_fifo->l_start_frame * (p_fifo->l_frame_size >> (p_fifo->b_stereo)))))
-        * p_aout->l_rate) / l_rate) + 1;
-
-    /* We release the lock before leaving */
-    vlc_mutex_unlock( &p_fifo->data_lock );
-    return( 0 );
 }
 
-void aout_Thread_S8_Mono( aout_thread_t * p_aout )
+/*****************************************************************************
+ * 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 );
 }
 
-void aout_Thread_S8_Stereo( aout_thread_t * p_aout )
+/*****************************************************************************
+ * 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;
+    }
 
-void aout_Thread_U8_Mono( aout_thread_t * p_aout )
-{
+    return p_buffer;
 }
 
-void aout_Thread_U8_Stereo( aout_thread_t * p_aout )
+/*****************************************************************************
+ * aout_FifoDestroy : destroy a FIFO and its buffers
+ *****************************************************************************/
+void aout_FifoDestroy( aout_instance_t * p_aout, aout_fifo_t * p_fifo )
 {
-    int i_fifo;
-    long l_buffer, l_buffer_limit;
-    long l_units, l_bytes;
-
-    intf_DbgMsg("adec debug: ********aout_Thread_U8_Stereo********\n");
-    intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
+    aout_buffer_t * p_buffer;
 
-    /* As the s32_buffer was created with calloc(), we don't have to set this
-     * memory to zero and we can immediately jump into the thread's loop */
-    while ( !p_aout->b_die )
+    p_buffer = p_fifo->p_first;
+    while ( p_buffer != NULL )
     {
-        vlc_mutex_lock( &p_aout->fifos_lock );
-        for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
-        {
-            switch ( p_aout->fifo[i_fifo].i_type )
-            {
-                case AOUT_EMPTY_FIFO:
-                    break;
-
-                case AOUT_INTF_MONO_FIFO:
-                    if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
-                    }
-                    else
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        free( p_aout->fifo[i_fifo].buffer ); /* !! */
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
-                    }
-                    break;
-
-                case AOUT_INTF_STEREO_FIFO:
-                    if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
-                    }
-                    else
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        free( p_aout->fifo[i_fifo].buffer ); /* !! */
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
-                    }
-                    break;
-
-                case AOUT_ADEC_MONO_FIFO:
-                    if ( p_aout->fifo[i_fifo].b_die )
-                    {
-                        free( p_aout->fifo[i_fifo].buffer );
-                        free( p_aout->fifo[i_fifo].date );
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                        continue;
-                    }
-
-                    l_units = p_aout->l_units;
-                    l_buffer = 0;
-                    while ( l_units > 0 )
-                    {
-                        if ( !p_aout->fifo[i_fifo].b_next_frame )
-                        {
-                            if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
-                            {
-                                break;
-                            }
-                        }
-
-                        if ( p_aout->fifo[i_fifo].l_units > l_units )
-                        {
-                            l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
-                                }
-                            }
-                            p_aout->fifo[i_fifo].l_units -= l_units;
-                            break;
-                        }
-                        else
-                        {
-                            l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
-                            /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
-                                }
-                            }
-                            l_units -= p_aout->fifo[i_fifo].l_units;
-
-                            vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
-                            p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
-                            vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
-                            vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
-
-                            /* p_aout->fifo[i_fifo].b_start_frame = 1; */
-                            p_aout->fifo[i_fifo].l_next_frame += 1;
-                            p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
-                            p_aout->fifo[i_fifo].b_next_frame = 0;
-                        }
-                    }
-                    break;
-
-                case AOUT_ADEC_STEREO_FIFO:
-                    if ( p_aout->fifo[i_fifo].b_die )
-                    {
-                        free( p_aout->fifo[i_fifo].buffer );
-                        free( p_aout->fifo[i_fifo].date );
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                        continue;
-                    }
-
-                    l_units = p_aout->l_units;
-                    l_buffer = 0;
-                    while ( l_units > 0 )
-                    {
-                        if ( !p_aout->fifo[i_fifo].b_next_frame )
-                        {
-                            if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
-                            {
-                                break;
-                            }
-                        }
-
-                        if ( p_aout->fifo[i_fifo].l_units > l_units )
-                        {
-                            l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
-                                }
-                            }
-                            p_aout->fifo[i_fifo].l_units -= l_units;
-                            break;
-                        }
-                        else
-                        {
-                            l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
-                            /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
-                                }
-                            }
-                            l_units -= p_aout->fifo[i_fifo].l_units;
-
-                            vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
-                            p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
-                            vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
-                            vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
-
-                            /* p_aout->fifo[i_fifo].b_start_frame = 1; */
-                            p_aout->fifo[i_fifo].l_next_frame += 1;
-                            p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
-                            p_aout->fifo[i_fifo].b_next_frame = 0;
-                        }
-                    }
-                    break;
-
-            default:
-                    intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
-                    break;
-            }
-        }
-        vlc_mutex_unlock( &p_aout->fifos_lock );
-
-        l_buffer_limit = p_aout->l_units  << 1 ; /* p_aout->b_stereo == 1 */
-
-        for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
-        {
-            ((u8 *)p_aout->buffer)[l_buffer] = (u8)( (p_aout->s32_buffer[l_buffer] / 256) + 128 );
-            p_aout->s32_buffer[l_buffer] = 0;
-        }
-        l_bytes = p_aout->p_sys_getbufinfo( p_aout, l_buffer_limit );
-        p_aout->date = mdate() + ((((mtime_t)(l_bytes / 2 )) * 1000000) / ((mtime_t)p_aout->l_rate)); /* sizeof(u8) << (p_aout->b_stereo) == 2 */
-        p_aout->p_sys_playsamples( p_aout, (byte_t *)p_aout->buffer, l_buffer_limit * sizeof(u8) );
-        if ( l_bytes > (l_buffer_limit * sizeof(u8)) )
-        {
-            msleep( p_aout->l_msleep );
-        }
+        aout_buffer_t * p_next = p_buffer->p_next;
+        aout_BufferFree( p_buffer );
+        p_buffer = p_next;
     }
+}
 
-    vlc_mutex_lock( &p_aout->fifos_lock );
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
-    {
-        switch ( p_aout->fifo[i_fifo].i_type )
-        {
-            case AOUT_EMPTY_FIFO:
-                break;
-
-            case AOUT_INTF_MONO_FIFO:
-            case AOUT_INTF_STEREO_FIFO:
-                free( p_aout->fifo[i_fifo].buffer ); /* !! */
-                p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                break;
-
-            case AOUT_ADEC_MONO_FIFO:
-            case AOUT_ADEC_STEREO_FIFO:
-                free( p_aout->fifo[i_fifo].buffer );
-                free( p_aout->fifo[i_fifo].date );
-                p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                break;
 
-            default:
-                break;
-        }
-    }
-    vlc_mutex_unlock( &p_aout->fifos_lock );
+/*
+ * 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;
 }
 
-void aout_Thread_S16_Mono( aout_thread_t * p_aout )
+/*****************************************************************************
+ * 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;
 }
 
-void aout_Thread_S16_Stereo( aout_thread_t * p_aout )
+/*****************************************************************************
+ * aout_DateMove : move forwards or backwards the date of an audio_date_t
+ *****************************************************************************/
+void aout_DateMove( audio_date_t * p_date, mtime_t difference )
 {
-    int i_fifo;
-    long l_buffer, l_buffer_limit;
-    long l_units, l_bytes;
-
-    intf_DbgMsg("adec debug: ********aout_Thread_S16_Stereo********\n");
-    intf_DbgMsg("adec debug: running audio output thread (%p) (pid == %i)\n", p_aout, getpid());
-
-    /* As the s32_buffer was created with calloc(), we don't have to set this
-     * memory to zero and we can immediately jump into the thread's loop */
-    while ( !p_aout->b_die )
-    {
-        vlc_mutex_lock( &p_aout->fifos_lock );
-        for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
-        {
-            switch ( p_aout->fifo[i_fifo].i_type )
-            {
-                case AOUT_EMPTY_FIFO:
-                    break;
-
-                case AOUT_INTF_MONO_FIFO:
-                    if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
-                    }
-                    else
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        free( p_aout->fifo[i_fifo].buffer ); /* !! */
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
-                    }
-                    break;
-
-                case AOUT_INTF_STEREO_FIFO:
-                    if ( p_aout->fifo[i_fifo].l_units > p_aout->l_units )
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        p_aout->fifo[i_fifo].l_units -= p_aout->l_units;
-                    }
-                    else
-                    {
-                        l_buffer = 0;
-                        while ( l_buffer < (p_aout->fifo[i_fifo].l_units << 1) ) /* p_aout->b_stereo == 1 */
-                        {
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                            p_aout->s32_buffer[l_buffer++] +=
-                                (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-                            UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                        }
-                        free( p_aout->fifo[i_fifo].buffer ); /* !! */
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]); /* !! */
-                    }
-                    break;
-
-                case AOUT_ADEC_MONO_FIFO:
-                    if ( p_aout->fifo[i_fifo].b_die )
-                    {
-                        free( p_aout->fifo[i_fifo].buffer );
-                        free( p_aout->fifo[i_fifo].date );
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                        continue;
-                    }
-
-                    l_units = p_aout->l_units;
-                    l_buffer = 0;
-                    while ( l_units > 0 )
-                    {
-                        if ( !p_aout->fifo[i_fifo].b_next_frame )
-                        {
-                            if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
-                            {
-                                break;
-                            }
-                        }
-
-                        if ( p_aout->fifo[i_fifo].l_units > l_units )
-                        {
-                            l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
-                                }
-                            }
-                            p_aout->fifo[i_fifo].l_units -= l_units;
-                            break;
-                        }
-                        else
-                        {
-                            l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
-                            /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[p_aout->fifo[i_fifo].l_unit] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 0 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 0));
-                                }
-                            }
-                            l_units -= p_aout->fifo[i_fifo].l_units;
-
-                            vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
-                            p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
-                            vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
-                            vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
-
-                            /* p_aout->fifo[i_fifo].b_start_frame = 1; */
-                            p_aout->fifo[i_fifo].l_next_frame += 1;
-                            p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
-                            p_aout->fifo[i_fifo].b_next_frame = 0;
-                        }
-                    }
-                    break;
-
-                case AOUT_ADEC_STEREO_FIFO:
-                    if ( p_aout->fifo[i_fifo].b_die )
-                    {
-                        free( p_aout->fifo[i_fifo].buffer );
-                        free( p_aout->fifo[i_fifo].date );
-                        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                        intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                        continue;
-                    }
-
-                    l_units = p_aout->l_units;
-                    l_buffer = 0;
-                    while ( l_units > 0 )
-                    {
-                        if ( !p_aout->fifo[i_fifo].b_next_frame )
-                        {
-                            if ( NextFrame(p_aout, &p_aout->fifo[i_fifo], p_aout->date + ((((mtime_t)(l_buffer >> 1)) * 1000000) / ((mtime_t)p_aout->l_rate))) )
-                            {
-                                break;
-                            }
-                        }
-
-                        if ( p_aout->fifo[i_fifo].l_units > l_units )
-                        {
-                            l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
-                                }
-                            }
-                            p_aout->fifo[i_fifo].l_units -= l_units;
-                            break;
-                        }
-                        else
-                        {
-                            l_buffer_limit = l_buffer + (p_aout->fifo[i_fifo].l_units << 1);
-                            /* p_aout->b_stereo == 1 */
-                            while ( l_buffer < l_buffer_limit )
-                            {
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit] );
-                                p_aout->s32_buffer[l_buffer++] +=
-                                    (s32)( ((s16 *)p_aout->fifo[i_fifo].buffer)[2*p_aout->fifo[i_fifo].l_unit+1] );
-
-                                UPDATE_INCREMENT( p_aout->fifo[i_fifo].unit_increment, p_aout->fifo[i_fifo].l_unit )
-                                if ( p_aout->fifo[i_fifo].l_unit >= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                     ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1)) )
-                                {
-                                    p_aout->fifo[i_fifo].l_unit -= /* p_aout->fifo[i_fifo].b_stereo == 1 */
-                                        ((AOUT_FIFO_SIZE + 1) * (p_aout->fifo[i_fifo].l_frame_size >> 1));
-                                }
-                            }
-                            l_units -= p_aout->fifo[i_fifo].l_units;
-
-                            vlc_mutex_lock( &p_aout->fifo[i_fifo].data_lock );
-                            p_aout->fifo[i_fifo].l_start_frame = p_aout->fifo[i_fifo].l_next_frame;
-                            vlc_cond_signal( &p_aout->fifo[i_fifo].data_wait );
-                            vlc_mutex_unlock( &p_aout->fifo[i_fifo].data_lock );
-
-                            /* p_aout->fifo[i_fifo].b_start_frame = 1; */
-                            p_aout->fifo[i_fifo].l_next_frame += 1;
-                            p_aout->fifo[i_fifo].l_next_frame &= AOUT_FIFO_SIZE;
-                            p_aout->fifo[i_fifo].b_next_frame = 0;
-                        }
-                    }
-                    break;
-
-            default:
-                    intf_DbgMsg("aout debug: unknown fifo type (%i)\n", p_aout->fifo[i_fifo].i_type);
-                    break;
-            }
-        }
-        vlc_mutex_unlock( &p_aout->fifos_lock );
-
-        l_buffer_limit = p_aout->l_units << 1; /* p_aout->b_stereo == 1 */
-
-        for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
-        {
-            ((s16 *)p_aout->buffer)[l_buffer] = (s16)( p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS );
-            p_aout->s32_buffer[l_buffer] = 0;
-        }
-
-        l_bytes = p_aout->p_sys_getbufinfo( p_aout, l_buffer_limit );
-        p_aout->date = -1000000 + mdate() + ((((mtime_t)(l_bytes / 4)) * 1000000) / ((mtime_t)p_aout->l_rate)); /* sizeof(s16) << (p_aout->b_stereo) == 4 */
-        p_aout->p_sys_playsamples( p_aout, (byte_t *)p_aout->buffer, l_buffer_limit * sizeof(s16) );
-        if ( l_bytes > (l_buffer_limit * sizeof(s16)) )
-        {
-            msleep( p_aout->l_msleep );
-        }
-    }
-
-    vlc_mutex_lock( &p_aout->fifos_lock );
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
-    {
-        switch ( p_aout->fifo[i_fifo].i_type )
-        {
-            case AOUT_EMPTY_FIFO:
-                break;
-
-            case AOUT_INTF_MONO_FIFO:
-            case AOUT_INTF_STEREO_FIFO:
-                free( p_aout->fifo[i_fifo].buffer ); /* !! */
-                p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                break;
-
-            case AOUT_ADEC_MONO_FIFO:
-            case AOUT_ADEC_STEREO_FIFO:
-                free( p_aout->fifo[i_fifo].buffer );
-                free( p_aout->fifo[i_fifo].date );
-                p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO; /* !! */
-                intf_DbgMsg("aout debug: audio output fifo (%p) destroyed\n", &p_aout->fifo[i_fifo]);
-                break;
-
-            default:
-                break;
-        }
-    }
-    vlc_mutex_unlock( &p_aout->fifos_lock );
+    p_date->date += difference;
 }
 
-void aout_Thread_U16_Mono( aout_thread_t * p_aout )
+/*****************************************************************************
+ * aout_DateGet : get the date of an audio_date_t
+ *****************************************************************************/
+mtime_t aout_DateGet( const audio_date_t * p_date )
 {
+    return p_date->date;
 }
 
-void aout_Thread_U16_Stereo( aout_thread_t * 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;
 }
+