Commit c1aa8904 authored by Cyril Deguet's avatar Cyril Deguet

- le coup de gr�ce: all the code in audio output is now factorized (except
for ac3 spdif) in aout_common.c/.h
- aout thread functions are declared with a macro

TODO:
- test unsigned 8 bit output, and implement S8 and U16 outputs
- multi-channel support
- check if resampling takes the PTS into account (and fix it)
parent d33deda2
......@@ -126,7 +126,7 @@ PLUGINS_TARGETS := ac3_adec/ac3_adec \
INTERFACE := main interface intf_msg intf_playlist intf_eject
INPUT := input input_ext-dec input_ext-intf input_dec input_programs input_clock mpeg_system
VIDEO_OUTPUT := video_output video_text vout_pictures vout_subpictures
AUDIO_OUTPUT := audio_output aout_common aout_ext-dec aout_u8 aout_s8 aout_u16 aout_s16 aout_spdif
AUDIO_OUTPUT := audio_output aout_common aout_ext-dec aout_spdif
MISC := mtime modules netutils iso_lang
C_OBJ := $(INTERFACE:%=src/interface/%.o) \
......
......@@ -2,7 +2,7 @@
* aout_common.c: generic audio output functions
*****************************************************************************
* Copyright (C) 1999-2002 VideoLAN
* $Id
* $Id: aout_common.c,v 1.2 2002/01/14 19:54:36 asmax Exp $
*
* Authors: Michel Kaempf <maxx@via.ecp.fr>
* Cyril Deguet <asmax@via.ecp.fr>
......@@ -34,6 +34,13 @@
#include "audio_output.h"
#include "aout_common.h"
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo,
mtime_t aout_date );
/*****************************************************************************
* Functions
*****************************************************************************/
......@@ -82,7 +89,8 @@ void aout_FillBuffer( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
(s32)( ((s16 *)p_fifo->buffer)[p_fifo->l_unit] );
}
UPDATE_INCREMENT( p_fifo->unit_increment, p_fifo->l_unit )
UpdateIncrement(&p_fifo->unit_increment, &p_fifo->l_unit);
if( p_fifo->l_unit >= ((AOUT_FIFO_SIZE + 1) *
(p_fifo->l_frame_size >> p_fifo->b_stereo)) )
{
......@@ -91,12 +99,12 @@ void aout_FillBuffer( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
}
}
if ( p_fifo->l_units > l_units )
if( p_fifo->l_units > l_units )
{
p_fifo->l_units -= l_units;
break;
}
else /* p_fifo->l_units <= l_units */
else
{
l_units -= p_fifo->l_units;
......@@ -120,3 +128,93 @@ void aout_FillBuffer( aout_thread_t * p_aout, aout_fifo_t * p_fifo )
}
}
/* Following functions are local */
static int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo,
mtime_t aout_date )
{
long l_units, l_rate;
u64 l_delta;
/* We take the lock */
vlc_mutex_lock( &p_fifo->data_lock );
/* Are we looking for a dated start frame ? */
if ( !p_fifo->b_start_frame )
{
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 );
}
}
/* 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;
}
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 );
}
}
}
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_delta = aout_date - p_fifo->date[p_fifo->l_start_frame];
/* Resample if delta is too long */
if( abs(l_delta) > MAX_DELTA )
{
l_rate = p_fifo->l_rate + (l_delta / 256);
}
else
{
l_rate = p_fifo->l_rate;
}
intf_DbgMsg( "aout debug: %lli (%li);", 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 );
}
......@@ -2,7 +2,7 @@
* aout_common.h: audio output inner functions
*****************************************************************************
* Copyright (C) 1999, 2000, 2001 VideoLAN
* $Id: aout_common.h,v 1.7 2002/01/14 12:15:10 asmax Exp $
* $Id: aout_common.h,v 1.8 2002/01/14 19:54:36 asmax Exp $
*
* Authors: Michel Kaempf <maxx@via.ecp.fr>
* Cyril Deguet <asmax@via.ecp.fr>
......@@ -39,16 +39,70 @@ void aout_SpdifThread ( aout_thread_t * p_aout );
void aout_FillBuffer ( aout_thread_t * p_aout, aout_fifo_t * p_fifo );
#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; \
}
/* Generic main thread function "aout_XXXThread"
*/
#define DECLARE_AOUT_THREAD( format, type, buffer_copy ) \
void aout_##format##Thread( aout_thread_t * p_aout ) \
{ \
\
int i_fifo; \
long l_buffer, l_buffer_limit, l_bytes; \
\
/* 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++ ) \
{ \
if( p_aout->fifo[i_fifo].b_die ) \
{ \
aout_FreeFifo( &p_aout->fifo[i_fifo] ); \
} \
else \
{ \
aout_FillBuffer( p_aout, &p_aout->fifo[i_fifo] ); \
} \
} \
\
vlc_mutex_unlock( &p_aout->fifos_lock ); \
\
l_buffer_limit = p_aout->l_units << p_aout->b_stereo; \
\
for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ ) \
{ \
((type *)p_aout->buffer)[l_buffer] = (type)( buffer_copy ); \
p_aout->s32_buffer[l_buffer] = 0; \
} \
\
l_bytes = p_aout->pf_getbufinfo( p_aout, l_buffer_limit ); \
\
p_aout->date = mdate() + ((((mtime_t)((l_bytes + 4 * \
p_aout->i_latency) / \
(sizeof(type) << p_aout->b_stereo))) * 1000000) / \
((mtime_t)p_aout->l_rate)) + p_main->i_desync; \
\
p_aout->pf_play( p_aout, (byte_t *)p_aout->buffer, l_buffer_limit * \
sizeof(type) ); \
\
if ( l_bytes > (l_buffer_limit * sizeof(type)) ) \
{ \
msleep( p_aout->l_msleep ); \
} \
} \
\
vlc_mutex_lock( &p_aout->fifos_lock ); \
\
for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ ) \
{ \
aout_FreeFifo( &p_aout->fifo[i_fifo] ); \
} \
\
vlc_mutex_unlock( &p_aout->fifos_lock ); \
\
} \
/*****************************************************************************
* InitializeIncrement
......@@ -71,89 +125,21 @@ static __inline__ void InitializeIncrement( aout_increment_t * p_increment,
p_increment->l_euclidean_denominator = l_denominator;
}
/*****************************************************************************
* NextFrame
* UpdateIncrement
*****************************************************************************/
static __inline__ int NextFrame( aout_thread_t * p_aout, aout_fifo_t * p_fifo,
mtime_t aout_date )
static __inline__ void UpdateIncrement( aout_increment_t * p_increment,
long * p_integer )
{
long l_units, l_rate;
u64 l_delta;
/* We take the lock */
vlc_mutex_lock( &p_fifo->data_lock );
/* Are we looking for a dated start frame ? */
if ( !p_fifo->b_start_frame )
if( (p_increment->l_remainder += p_increment->l_euclidean_remainder) >= 0 )
{
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 );
}
}
/* 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;
}
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 );
}
}
}
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_delta = aout_date - p_fifo->date[p_fifo->l_start_frame];
/* Resample if delta is too long */
if( abs(l_delta) > MAX_DELTA )
{
l_rate = p_fifo->l_rate + (l_delta / 256);
*p_integer += p_increment->l_euclidean_integer + 1;
p_increment->l_remainder -= p_increment->l_euclidean_denominator;
}
else
{
l_rate = p_fifo->l_rate;
*p_integer += p_increment->l_euclidean_integer;
}
intf_DbgMsg( "aout debug: %lli (%li);", 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 );
}
/*****************************************************************************
* aout_s16.c: 16 bit signed audio output functions
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id
*
* Authors: Michel Kaempf <maxx@via.ecp.fr>
* Cyril Deguet <asmax@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.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdio.h> /* "intf_msg.h" */
#include <stdlib.h> /* calloc(), malloc(), free() */
#include <string.h>
#include <videolan/vlc.h>
#include "audio_output.h"
#include "aout_common.h"
/*****************************************************************************
* Functions
*****************************************************************************/
void aout_S16Thread( aout_thread_t * p_aout )
{
int i_fifo;
long l_buffer, l_buffer_limit, l_bytes;
/* 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++ )
{
if( p_aout->fifo[i_fifo].b_die )
{
aout_FreeFifo( &p_aout->fifo[i_fifo] );
}
else
{
aout_FillBuffer( p_aout, &p_aout->fifo[i_fifo] );
}
}
vlc_mutex_unlock( &p_aout->fifos_lock );
l_buffer_limit = p_aout->l_units << p_aout->b_stereo;
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->i_volume / 256 ) ;
p_aout->s32_buffer[l_buffer] = 0;
}
l_bytes = p_aout->pf_getbufinfo( p_aout, l_buffer_limit );
p_aout->date = mdate() + ((((mtime_t)((l_bytes + 4 * p_aout->i_latency) /
(sizeof(s16) << p_aout->b_stereo))) * 1000000) /
((mtime_t)p_aout->l_rate)) + p_main->i_desync;
p_aout->pf_play( 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++ )
{
aout_FreeFifo( &p_aout->fifo[i_fifo] );
}
vlc_mutex_unlock( &p_aout->fifos_lock );
}
/*****************************************************************************
* aout_s8.c: 8 bit signed audio output functions
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: aout_s8.c,v 1.6 2002/01/14 12:15:10 asmax Exp $
*
* Authors: Michel Kaempf <maxx@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.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdio.h> /* "intf_msg.h" */
#include <stdlib.h> /* calloc(), malloc(), free() */
#include <string.h>
#include <videolan/vlc.h>
#include "audio_output.h"
#include "aout_common.h"
/*****************************************************************************
* Functions
*****************************************************************************/
void aout_S8Thread( aout_thread_t * p_aout )
{
intf_ErrMsg( "aout error: 8 bit signed thread unsupported" );
}
/*****************************************************************************
* aout_u16.c: 16 bit unsigned audio output functions
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: aout_u16.c,v 1.6 2002/01/14 12:15:10 asmax Exp $
*
* Authors: Michel Kaempf <maxx@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.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdio.h> /* "intf_msg.h" */
#include <stdlib.h> /* calloc(), malloc(), free() */
#include <string.h>
#include <videolan/vlc.h>
#include "audio_output.h"
#include "aout_common.h"
/*****************************************************************************
* Functions
*****************************************************************************/
void aout_U16Thread( aout_thread_t * p_aout )
{
intf_ErrMsg( "aout error: 16 bit unsigned thread unsupported" );
}
/*****************************************************************************
* aout_u8.c: 8 bit unsigned audio output functions
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: aout_u8.c,v 1.11 2002/01/14 12:15:10 asmax Exp $
*
* Authors: Michel Kaempf <maxx@via.ecp.fr>
* Cyril Deguet <asmax@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.
*
* 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.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#include <stdio.h> /* "intf_msg.h" */
#include <stdlib.h> /* calloc(), malloc(), free() */
#include <string.h>
#include <videolan/vlc.h>
#include "audio_output.h"
#include "aout_common.h"
/*****************************************************************************
* Functions
*****************************************************************************/
void aout_U8Thread( aout_thread_t * p_aout )
{
int i_fifo;
long l_buffer, l_buffer_limit, l_bytes;
/* 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++ )
{
if( p_aout->fifo[i_fifo].b_die )
{
aout_FreeFifo( &p_aout->fifo[i_fifo] );
}
else
{
aout_FillBuffer( p_aout, &p_aout->fifo[i_fifo] );
}
}
vlc_mutex_unlock( &p_aout->fifos_lock );
l_buffer_limit = p_aout->l_units << p_aout->b_stereo;
for ( l_buffer = 0; l_buffer < l_buffer_limit; l_buffer++ )
{
((u8 *)p_aout->buffer)[l_buffer] =
(u8)( ( (p_aout->s32_buffer[l_buffer] / AOUT_MAX_FIFOS / 256)
+ 128 ) * p_aout->i_volume / 256 );
p_aout->s32_buffer[l_buffer] = 0;
}
l_bytes = p_aout->pf_getbufinfo( p_aout, l_buffer_limit );
p_aout->date = mdate() + ((((mtime_t)((l_bytes + 4 * p_aout->i_latency) /
sizeof(u8) << (p_aout->b_stereo))) * 1000000) /
((mtime_t)p_aout->l_rate)) + p_main->i_desync;
p_aout->pf_play( 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 );
}
}
vlc_mutex_lock( &p_aout->fifos_lock );
for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
{
aout_FreeFifo( &p_aout->fifo[i_fifo] );
}
vlc_mutex_unlock( &p_aout->fifos_lock );
}
......@@ -2,7 +2,7 @@
* audio_output.c : audio output thread
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: audio_output.c,v 1.71 2002/01/14 12:15:10 asmax Exp $
* $Id: audio_output.c,v 1.72 2002/01/14 19:54:36 asmax Exp $
*
* Authors: Michel Kaempf <maxx@via.ecp.fr>
* Cyril Deguet <asmax@via.ecp.fr>
......@@ -172,6 +172,27 @@ aout_thread_t *aout_CreateThread( int *pi_status, int i_channels, long l_rate )
return( p_aout );
}
/*****************************************************************************
* Declare the different aout thread fucntions
*****************************************************************************/
DECLARE_AOUT_THREAD( S16, s16, ( p_aout->s32_buffer[l_buffer] /
AOUT_MAX_FIFOS ) * p_aout->i_volume / 256 )
DECLARE_AOUT_THREAD( U8, u8, (( p_aout->s32_buffer[l_buffer] /
AOUT_MAX_FIFOS / 256) + 128 ) * p_aout->i_volume / 256 )