Commit 402c5320 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

aout mixer: do output buffer packetization in core, simplify

Now the mixer can focus on applying to volume. Dummy mixers for
S/PDIF and linear are now merged.
parent fefde73e
...@@ -72,8 +72,8 @@ struct aout_mixer_t { ...@@ -72,8 +72,8 @@ struct aout_mixer_t {
/* Array of mixer inputs */ /* Array of mixer inputs */
aout_mixer_input_t *input; aout_mixer_input_t *input;
/* Mix requested number of samples (mandatory) */ /* Mix buffer (mandatory) */
aout_buffer_t *(*mix)(aout_mixer_t *, unsigned, float); void (*mix)(aout_mixer_t *, aout_buffer_t *, float);
/* Private place holder for the aout_mixer_t module (optional) /* Private place holder for the aout_mixer_t module (optional)
* *
......
SOURCES_trivial_mixer = trivial.c SOURCES_trivial_mixer = trivial.c
SOURCES_float32_mixer = float32.c SOURCES_float32_mixer = float32.c
SOURCES_spdif_mixer = spdif.c
libvlc_LTLIBRARIES += \ libvlc_LTLIBRARIES += \
libfloat32_mixer_plugin.la \ libfloat32_mixer_plugin.la \
libspdif_mixer_plugin.la \
libtrivial_mixer_plugin.la libtrivial_mixer_plugin.la
...@@ -38,8 +38,8 @@ ...@@ -38,8 +38,8 @@
/***************************************************************************** /*****************************************************************************
* Local prototypes * Local prototypes
*****************************************************************************/ *****************************************************************************/
static int Create ( vlc_object_t * ); static int Create( vlc_object_t * );
static aout_buffer_t *DoWork( aout_mixer_t *, unsigned, float ); static void DoWork( aout_mixer_t *, aout_buffer_t *, float );
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
...@@ -52,9 +52,9 @@ vlc_module_begin () ...@@ -52,9 +52,9 @@ vlc_module_begin ()
set_callbacks( Create, NULL ) set_callbacks( Create, NULL )
vlc_module_end () vlc_module_end ()
/***************************************************************************** /**
* Create: allocate mixer * Initializes the mixer
*****************************************************************************/ */
static int Create( vlc_object_t *p_this ) static int Create( vlc_object_t *p_this )
{ {
aout_mixer_t * p_mixer = (aout_mixer_t *)p_this; aout_mixer_t * p_mixer = (aout_mixer_t *)p_this;
...@@ -66,76 +66,18 @@ static int Create( vlc_object_t *p_this ) ...@@ -66,76 +66,18 @@ static int Create( vlc_object_t *p_this )
return 0; return 0;
} }
/***************************************************************************** /**
* ScaleWords: prepare input words for averaging * Mixes a new output buffer
*****************************************************************************/ */
static void ScaleWords( float * p_out, const float * p_in, size_t i_nb_words, static void DoWork( aout_mixer_t * p_mixer, aout_buffer_t *p_buffer,
float f_multiplier ) float f_multiplier )
{
if( f_multiplier == 1.0 )
{
vlc_memcpy( p_out, p_in, i_nb_words * sizeof(float) );
return;
}
for( size_t i = 0; i < i_nb_words; i++ )
*p_out++ = *p_in++ * f_multiplier;
}
/*****************************************************************************
* DoWork: mix a new output buffer
*****************************************************************************
* Terminology : in this function a word designates a single float32, eg.
* a stereo sample is consituted of two words.
*****************************************************************************/
static aout_buffer_t *DoWork( aout_mixer_t * p_mixer, unsigned samples,
float f_multiplier )
{ {
aout_mixer_input_t * p_input = p_mixer->input; f_multiplier *= p_mixer->input->multiplier;
const int i_nb_channels = aout_FormatNbChannels( &p_mixer->fmt );
int i_nb_words = samples * i_nb_channels;
block_t *p_buffer = block_Alloc( i_nb_words * sizeof(float) );
if( unlikely( p_buffer == NULL ) )
return NULL;
p_buffer->i_nb_samples = samples;
float * p_out = (float *)p_buffer->p_buffer;
float * p_in = (float *)p_input->begin;
f_multiplier *= p_input->multiplier; if( f_multiplier == 1.0 )
return; /* nothing to do */
for( ; ; )
{
ptrdiff_t i_available_words = (
(float *)p_input->fifo.p_first->p_buffer - p_in)
+ p_input->fifo.p_first->i_nb_samples
* i_nb_channels;
if( i_available_words < i_nb_words )
{
aout_buffer_t * p_old_buffer;
ScaleWords( p_out, p_in, i_available_words, f_multiplier );
i_nb_words -= i_available_words;
p_out += i_available_words;
/* Next buffer */ float *p = (float *)p_buffer->p_buffer;
p_old_buffer = aout_FifoPop( &p_input->fifo ); for( size_t i = p_buffer->i_buffer / sizeof(float); i > 0; i-- )
aout_BufferFree( p_old_buffer ); *(p++) *= f_multiplier;
if( p_input->fifo.p_first == NULL )
{
msg_Err( p_mixer, "internal amix error" );
break;
}
p_in = (float *)p_input->fifo.p_first->p_buffer;
}
else
{
ScaleWords( p_out, p_in, i_nb_words, f_multiplier );
p_input->begin = (void *)(p_in + i_nb_words);
break;
}
}
return p_buffer;
} }
/*****************************************************************************
* spdif.c : dummy mixer for S/PDIF output (1 input only)
*****************************************************************************
* Copyright (C) 2002 the VideoLAN team
* $Id$
*
* 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.
*
* 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*****************************************************************************
* Preamble
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_aout.h>
#include <vlc_aout_mixer.h>
/*****************************************************************************
* Local prototypes
*****************************************************************************/
static int Create ( vlc_object_t * );
static aout_buffer_t *DoWork( aout_mixer_t *, unsigned, float );
/*****************************************************************************
* Module descriptor
*****************************************************************************/
vlc_module_begin ()
set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_MISC )
set_description( N_("Dummy S/PDIF audio mixer") )
set_capability( "audio mixer", 1 )
set_callbacks( Create, NULL )
vlc_module_end ()
/*****************************************************************************
* Create: allocate spdif mixer
*****************************************************************************/
static int Create( vlc_object_t *p_this )
{
aout_mixer_t *p_mixer = (aout_mixer_t *)p_this;
if ( !AOUT_FMT_NON_LINEAR(&p_mixer->fmt) )
return -1;
p_mixer->mix = DoWork;
return 0;
}
/*****************************************************************************
* DoWork: mix a new output buffer - this does nothing, indeed
*****************************************************************************/
static aout_buffer_t *DoWork( aout_mixer_t * p_mixer, unsigned samples,
float multiplier )
{
aout_mixer_input_t * p_input = p_mixer->input;
aout_buffer_t * p_old_buffer = aout_FifoPop( &p_input->fifo );
(void) samples; (void) multiplier;
return p_old_buffer;
}
...@@ -29,7 +29,6 @@ ...@@ -29,7 +29,6 @@
# include "config.h" # include "config.h"
#endif #endif
#include <stddef.h>
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_plugin.h> #include <vlc_plugin.h>
#include <vlc_aout.h> #include <vlc_aout.h>
...@@ -38,9 +37,8 @@ ...@@ -38,9 +37,8 @@
/***************************************************************************** /*****************************************************************************
* Local prototypes * Local prototypes
*****************************************************************************/ *****************************************************************************/
static int Create ( vlc_object_t * ); static int Create( vlc_object_t * );
static void DoNothing( aout_mixer_t *, aout_buffer_t *p_buffer, float );
static aout_buffer_t *DoWork( aout_mixer_t *, unsigned samples, float );
/***************************************************************************** /*****************************************************************************
* Module descriptor * Module descriptor
...@@ -48,7 +46,7 @@ static aout_buffer_t *DoWork( aout_mixer_t *, unsigned samples, float ); ...@@ -48,7 +46,7 @@ static aout_buffer_t *DoWork( aout_mixer_t *, unsigned samples, float );
vlc_module_begin () vlc_module_begin ()
set_category( CAT_AUDIO ) set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_MISC ) set_subcategory( SUBCAT_AUDIO_MISC )
set_description( N_("Trivial audio mixer") ) set_description( N_("Dummy audio mixer") )
set_capability( "audio mixer", 1 ) set_capability( "audio mixer", 1 )
set_callbacks( Create, NULL ) set_callbacks( Create, NULL )
vlc_module_end () vlc_module_end ()
...@@ -60,60 +58,14 @@ static int Create( vlc_object_t *p_this ) ...@@ -60,60 +58,14 @@ static int Create( vlc_object_t *p_this )
{ {
aout_mixer_t *p_mixer = (aout_mixer_t *)p_this; aout_mixer_t *p_mixer = (aout_mixer_t *)p_this;
if( AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) ) p_mixer->mix = DoNothing;
return -1;
p_mixer->mix = DoWork;
return 0; return 0;
} }
/***************************************************************************** static void DoNothing( aout_mixer_t *p_mixer, aout_buffer_t *p_buffer,
* DoWork: mix a new output buffer float multiplier )
*****************************************************************************/
static aout_buffer_t *DoWork( aout_mixer_t *p_mixer, unsigned samples,
float multiplier )
{ {
aout_mixer_input_t *p_input = p_mixer->input; (void) p_mixer;
unsigned framesize = aout_FormatNbChannels( &p_mixer->fmt ) (void) p_buffer;
* (p_mixer->fmt.i_bitspersample / 8);
size_t needed = samples * framesize;
aout_buffer_t *p_buffer = block_Alloc( needed );
if( unlikely(p_buffer == NULL) )
return NULL;
p_buffer->i_nb_samples = samples;
uint8_t * p_in = p_input->begin;
uint8_t * p_out = p_buffer->p_buffer;
for ( ; ; )
{
size_t avail = p_input->fifo.p_first->i_nb_samples * framesize
- (p_in - p_input->fifo.p_first->p_buffer);
if ( avail < needed )
{
vlc_memcpy( p_out, p_in, avail );
needed -= avail;
p_out += avail;
/* Next buffer */
aout_buffer_t *p_old_buffer = aout_FifoPop( &p_input->fifo );
aout_BufferFree( p_old_buffer );
if ( p_input->fifo.p_first == NULL )
{
msg_Err( p_mixer, "internal amix error" );
break;
}
p_in = p_input->fifo.p_first->p_buffer;
}
else
{
vlc_memcpy( p_out, p_in, needed );
p_input->begin = p_in + needed;
break;
}
}
(void) multiplier; (void) multiplier;
return p_buffer;
} }
...@@ -190,15 +190,14 @@ static int MixBuffer( aout_instance_t * p_aout, float volume ) ...@@ -190,15 +190,14 @@ static int MixBuffer( aout_instance_t * p_aout, float volume )
prev_date = p_buffer->i_pts + p_buffer->i_length; prev_date = p_buffer->i_pts + p_buffer->i_length;
} }
p_buffer = p_fifo->p_first;
if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) ) if( !AOUT_FMT_NON_LINEAR( &p_mixer->fmt ) )
{ {
p_buffer = p_fifo->p_first;
/* Additionally check that p_first_byte_to_mix is well located. */ /* Additionally check that p_first_byte_to_mix is well located. */
const unsigned framesize = p_mixer->fmt.i_bytes_per_frame;
mtime_t i_buffer = (start_date - p_buffer->i_pts) mtime_t i_buffer = (start_date - p_buffer->i_pts)
* p_mixer->fmt.i_bytes_per_frame * framesize * p_mixer->fmt.i_rate / CLOCK_FREQ;
* p_mixer->fmt.i_rate
/ p_mixer->fmt.i_frame_length
/ CLOCK_FREQ;
if( p_input->begin == NULL ) if( p_input->begin == NULL )
p_input->begin = p_buffer->p_buffer; p_input->begin = p_buffer->p_buffer;
...@@ -223,17 +222,57 @@ static int MixBuffer( aout_instance_t * p_aout, float volume ) ...@@ -223,17 +222,57 @@ static int MixBuffer( aout_instance_t * p_aout, float volume )
} }
p_input->begin = p_buffer->p_buffer + i_buffer; p_input->begin = p_buffer->p_buffer + i_buffer;
} }
}
/* Run the mixer. */ /* Build packet with adequate number of samples */
p_buffer = p_mixer->mix( p_mixer, p_aout->output.i_nb_samples, volume ); const unsigned samples = p_aout->output.i_nb_samples;
aout_unlock_input_fifos( p_aout ); unsigned needed = samples * framesize;
p_buffer = block_Alloc( needed );
if( unlikely(p_buffer == NULL) )
/* XXX: should free input buffers */
goto giveup;
p_buffer->i_nb_samples = samples;
uint8_t *p_in = p_input->begin;
uint8_t *p_out = p_buffer->p_buffer;
for( ;; )
{
size_t avail = p_fifo->p_first->i_nb_samples * framesize
- (p_in - p_fifo->p_first->p_buffer);
if( avail < needed )
{
vlc_memcpy( p_out, p_in, avail );
needed -= avail;
p_out += avail;
if( unlikely(p_buffer == NULL) ) /* Next buffer */
return -1; aout_BufferFree( aout_FifoPop( p_fifo ) );
if( p_input->fifo.p_first == NULL )
{
msg_Err( p_mixer, "internal amix error" );
vlc_memset( p_out, 0, needed );
break;
}
p_in = p_fifo->p_first->p_buffer;
}
else
{
vlc_memcpy( p_out, p_in, needed );
p_input->begin = p_in + needed;
break;
}
}
}
else
p_buffer = aout_FifoPop( p_fifo );
p_buffer->i_pts = start_date; p_buffer->i_pts = start_date;
p_buffer->i_length = end_date - start_date; p_buffer->i_length = end_date - start_date;
/* Run the mixer. */
p_mixer->mix( p_mixer, p_buffer, volume );
aout_unlock_input_fifos( p_aout );
aout_OutputPlay( p_aout, p_buffer ); aout_OutputPlay( p_aout, p_buffer );
return 0; return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment