X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=modules%2Faudio_filter%2Fchannel_mixer%2Fsimple.c;h=59572ebd9e80bb0308de150d5ca8f4a8b231b3a9;hb=7222bb1d253707d8efa720a9f5f131fb31895c5b;hp=4c06bb9b07862a8aa97a56a51972d4b7b4dc6940;hpb=bfed7bf6858bc9f5bef9ceafee66abc5e4daaa7e;p=vlc diff --git a/modules/audio_filter/channel_mixer/simple.c b/modules/audio_filter/channel_mixer/simple.c index 4c06bb9b07..59572ebd9e 100644 --- a/modules/audio_filter/channel_mixer/simple.c +++ b/modules/audio_filter/channel_mixer/simple.c @@ -1,24 +1,24 @@ /***************************************************************************** * simple.c : simple channel mixer plug-in ***************************************************************************** - * Copyright (C) 2002, 2006 the VideoLAN team + * Copyright (C) 2002, 2004, 2006-2009 VLC authors and VideoLAN * $Id$ * * Authors: Gildas Bazin * - * 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 + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 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 Lesser 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. + * You should have received a copy of the GNU Lesser 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. *****************************************************************************/ /***************************************************************************** @@ -44,192 +44,198 @@ vlc_module_begin () set_description( N_("Audio filter for simple channel mixing") ) set_category( CAT_AUDIO ) set_subcategory( SUBCAT_AUDIO_MISC ) - set_capability( "audio filter2", 10 ) + set_capability( "audio converter", 10 ) set_callbacks( OpenFilter, NULL ) vlc_module_end () /***************************************************************************** * Local prototypes *****************************************************************************/ -#define AOUT_CHANS_STEREO_FRONT ( AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT ) -#define AOUT_CHANS_STEREO_REAR ( AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT ) -#define AOUT_CHANS_STEREO_MIDDLE (AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT ) - -#define AOUT_CHANS_2_0 AOUT_CHANS_STEREO_FRONT -#define AOUT_CHANS_3_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER ) -#define AOUT_CHANS_4_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR ) -#define AOUT_CHANS_4_0_MIDDLE ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_MIDDLE ) -#define AOUT_CHANS_4_CENTER_REAR (AOUT_CHANS_STEREO_FRONT | AOUT_CHAN_CENTER | AOUT_CHAN_REARCENTER) -#define AOUT_CHANS_5_0 ( AOUT_CHANS_4_0 | AOUT_CHAN_CENTER ) -#define AOUT_CHANS_5_0_MIDDLE ( AOUT_CHANS_4_0_MIDDLE | AOUT_CHAN_CENTER ) -#define AOUT_CHANS_6_0 ( AOUT_CHANS_STEREO_FRONT | AOUT_CHANS_STEREO_REAR | AOUT_CHANS_STEREO_MIDDLE ) -#define AOUT_CHANS_7_0 ( AOUT_CHANS_6_0 | AOUT_CHAN_CENTER ) - static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_output ); +struct filter_sys_t +{ + void (*pf_dowork)(filter_t *, block_t *, block_t * ); +}; + static block_t *Filter( filter_t *, block_t * ); -/***************************************************************************** - * DoWork: convert a buffer - *****************************************************************************/ -static void DoWork( filter_t * p_filter, - aout_buffer_t * p_in_buf, aout_buffer_t * p_out_buf ) -{ - const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels; +static void DoWork_7_x_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4; + *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4; - const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0; - const bool b_input_5_0 = !b_input_7_0 && - ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 || - (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE ); - const bool b_input_4_center_rear = !b_input_7_0 && !b_input_5_0 && - (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR; - const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear && - (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0; + p_src += 7; - int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio ); - int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio ); + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} + +static void DoWork_5_x_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { float *p_dest = (float *)p_out_buf->p_buffer; const float *p_src = (const float *)p_in_buf->p_buffer; - int i; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[0] + 0.7071 * (p_src[4] + p_src[2]); + *p_dest++ = p_src[1] + 0.7071 * (p_src[4] + p_src[3]); - p_out_buf->i_nb_samples = p_in_buf->i_nb_samples; - p_out_buf->i_buffer = p_in_buf->i_buffer * i_output_nb / i_input_nb; + p_src += 5; - if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 ) + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} + +static void DoWork_4_0_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + VLC_UNUSED(p_filter); + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) { - if( b_input_7_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 4 + p_src[4] / 4; - p_dest++; - *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 4 + p_src[5] / 4; - p_dest++; + *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[0]; + *p_dest++ = p_src[2] + p_src[3] + 0.5 * p_src[1]; + p_src += 4; + } +} - p_src += 7; +static void DoWork_3_x_to_2_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[2] + 0.5 * p_src[0]; + *p_dest++ = p_src[2] + 0.5 * p_src[1]; - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else if( b_input_5_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[4] + 0.5 * p_src[0] + 0.33 * p_src[2]; - p_dest++; - *p_dest = p_src[4] + 0.5 * p_src[1] + 0.33 * p_src[3]; - p_dest++; + p_src += 3; - p_src += 5; + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else if( b_input_3_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[2] + 0.5 * p_src[0]; - p_dest++; - *p_dest = p_src[2] + 0.5 * p_src[1]; - p_dest++; - - p_src += 3; - - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else if (b_input_4_center_rear) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[0]; - p_dest++; - *p_dest = p_src[2] + p_src[3] + 0.5 * p_src[1]; - p_dest++; - p_src += 4; - } +static void DoWork_7_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[6] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 8 + p_src[3] / 8 + p_src[4] / 8 + p_src[5] / 8; + + p_src += 7; + + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; } - else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER ) +} + +static void DoWork_5_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) { - if( b_input_7_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[6] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 8 + p_src[3] / 8 + p_src[4] / 8 + p_src[5] / 8; - p_dest++; + *p_dest++ = 0.7071 * (p_src[0] + p_src[1]) + p_src[4] + 0.5f * (p_src[2] + p_src[3]); - p_src += 7; + p_src += 5; - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else if( b_input_5_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[4] + p_src[0] / 4 + p_src[1] / 4 + p_src[2] / 6 + p_src[3] / 6; - p_dest++; + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} - p_src += 5; +static void DoWork_4_0_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + VLC_UNUSED(p_filter); + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[2] + p_src[3] + p_src[0] / 4 + p_src[1] / 4; + p_src += 4; + } +} - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else if( b_input_3_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[2] + p_src[0] / 4 + p_src[1] / 4; - p_dest++; +static void DoWork_3_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[2] + p_src[0] / 4 + p_src[1] / 4; - p_src += 3; + p_src += 3; - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[0] / 2 + p_src[1] / 2; - p_dest++; + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} + +static void DoWork_2_x_to_1_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + VLC_UNUSED(p_filter); + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[0] / 2 + p_src[1] / 2; - p_src += 2; - } + p_src += 2; } - else +} + +static void DoWork_7_x_to_4_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) { - assert( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0 ); - assert( b_input_7_0 || b_input_5_0 ); + *p_dest++ = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6; + *p_dest++ = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6; + *p_dest++ = p_src[2] / 6 + p_src[4]; + *p_dest++ = p_src[3] / 6 + p_src[5]; - if( b_input_7_0 ) - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[6] + 0.5 * p_src[0] + p_src[2] / 6; - p_dest++; - *p_dest = p_src[6] + 0.5 * p_src[1] + p_src[3] / 6; - p_dest++; - *p_dest = p_src[2] / 6 + p_src[4]; - p_dest++; - *p_dest = p_src[3] / 6 + p_src[5]; - p_dest++; - - p_src += 7; - - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } - else - for( i = p_in_buf->i_nb_samples; i--; ) - { - *p_dest = p_src[4] + 0.5 * p_src[0]; - p_dest++; - *p_dest = p_src[4] + 0.5 * p_src[1]; - p_dest++; - *p_dest = p_src[2]; - p_dest++; - *p_dest = p_src[3]; - p_dest++; - - p_src += 5; - - if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; - } + p_src += 7; + + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} + +static void DoWork_5_x_to_4_0( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[0] + p_src[4] * 0.7071; + *p_dest++ = p_src[1] + p_src[4] * 0.7071; + *p_dest++ = p_src[2]; + *p_dest++ = p_src[3]; + + p_src += 5; + + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; + } +} + +static void DoWork_7_x_to_5_x( filter_t * p_filter, block_t * p_in_buf, block_t * p_out_buf ) { + float *p_dest = (float *)p_out_buf->p_buffer; + const float *p_src = (const float *)p_in_buf->p_buffer; + for( int i = p_in_buf->i_nb_samples; i--; ) + { + *p_dest++ = p_src[0]; + *p_dest++ = p_src[1]; + *p_dest++ = (p_src[2] + p_src[4]) * 0.5; + *p_dest++ = (p_src[3] + p_src[5]) * 0.5; + *p_dest++ = p_src[6]; + + p_src += 7; + + if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE && + p_filter->fmt_out.audio.i_physical_channels & AOUT_CHAN_LFE ) + *p_dest++ = *p_src++; + else if( p_filter->fmt_in.audio.i_physical_channels & AOUT_CHAN_LFE ) p_src++; } } + /***************************************************************************** * OpenFilter: *****************************************************************************/ static int OpenFilter( vlc_object_t *p_this ) { filter_t *p_filter = (filter_t *)p_this; + filter_sys_t *p_sys; audio_format_t fmt_in = p_filter->fmt_in.audio; audio_format_t fmt_out = p_filter->fmt_out.audio; @@ -238,11 +244,62 @@ static int OpenFilter( vlc_object_t *p_this ) fmt_out.i_format = p_filter->fmt_out.i_codec; if( !IsSupported( &fmt_in, &fmt_out ) ) - return -1; + return VLC_EGENERIC; + + p_filter->p_sys = malloc( sizeof(*p_sys) ); + if( unlikely(!p_filter->p_sys) ) + return VLC_ENOMEM; p_filter->pf_audio_filter = Filter; - return 0; + const unsigned i_input_physical = p_filter->fmt_in.audio.i_physical_channels; + const bool b_input_7_0 = (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_7_0; + const bool b_input_5_0 = !b_input_7_0 && + ( (i_input_physical & AOUT_CHANS_5_0) == AOUT_CHANS_5_0 || + (i_input_physical & AOUT_CHANS_5_0_MIDDLE) == AOUT_CHANS_5_0_MIDDLE ); + const bool b_input_4_center_rear = !b_input_7_0 && !b_input_5_0 && + (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_4_CENTER_REAR; + const bool b_input_3_0 = !b_input_7_0 && !b_input_5_0 && !b_input_4_center_rear && + (i_input_physical & ~AOUT_CHAN_LFE) == AOUT_CHANS_3_0; + + if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_2_0 ) + { + if( b_input_7_0 ) + p_filter->p_sys->pf_dowork = DoWork_7_x_to_2_0; + else if( b_input_5_0 ) + p_filter->p_sys->pf_dowork = DoWork_5_x_to_2_0; + else if( b_input_4_center_rear ) + p_filter->p_sys->pf_dowork = DoWork_4_0_to_2_0; + else if( b_input_3_0 ) + p_filter->p_sys->pf_dowork = DoWork_3_x_to_2_0; + } + else if( p_filter->fmt_out.audio.i_physical_channels == AOUT_CHAN_CENTER ) + { + if( b_input_7_0 ) + p_filter->p_sys->pf_dowork = DoWork_7_x_to_1_0; + else if( b_input_5_0 ) + p_filter->p_sys->pf_dowork = DoWork_5_x_to_1_0; + else if( b_input_4_center_rear ) + p_filter->p_sys->pf_dowork = DoWork_4_0_to_1_0; + else if( b_input_3_0 ) + p_filter->p_sys->pf_dowork = DoWork_3_x_to_1_0; + else + p_filter->p_sys->pf_dowork = DoWork_2_x_to_1_0; + } + else if(p_filter->fmt_out.audio.i_physical_channels == AOUT_CHANS_4_0) + { + if( b_input_7_0 ) + p_filter->p_sys->pf_dowork = DoWork_7_x_to_4_0; + else + p_filter->p_sys->pf_dowork = DoWork_5_x_to_4_0; + } + else + { + assert( b_input_7_0 ); + p_filter->p_sys->pf_dowork = DoWork_7_x_to_5_x; + } + + return VLC_SUCCESS; } /***************************************************************************** @@ -250,6 +307,7 @@ static int OpenFilter( vlc_object_t *p_this ) *****************************************************************************/ static block_t *Filter( filter_t *p_filter, block_t *p_block ) { + filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys; if( !p_block || !p_block->i_nb_samples ) { if( p_block ) @@ -261,7 +319,7 @@ static block_t *Filter( filter_t *p_filter, block_t *p_block ) p_filter->fmt_out.audio.i_bitspersample * p_filter->fmt_out.audio.i_channels / 8; - block_t *p_out = filter_NewAudioBuffer( p_filter, i_out_size ); + block_t *p_out = block_Alloc( i_out_size ); if( !p_out ) { msg_Warn( p_filter, "can't get output buffer" ); @@ -274,7 +332,12 @@ static block_t *Filter( filter_t *p_filter, block_t *p_block ) p_out->i_pts = p_block->i_pts; p_out->i_length = p_block->i_length; - DoWork( p_filter, p_block, p_out ); + int i_input_nb = aout_FormatNbChannels( &p_filter->fmt_in.audio ); + int i_output_nb = aout_FormatNbChannels( &p_filter->fmt_out.audio ); + p_out->i_nb_samples = p_block->i_nb_samples; + p_out->i_buffer = p_block->i_buffer * i_output_nb / i_input_nb; + + p_sys->pf_dowork( p_filter, p_block, p_out ); block_Release( p_block ); @@ -300,7 +363,8 @@ static bool IsSupported( const audio_format_t *p_input, const audio_format_t *p_ /* Only conversion to Mono, Stereo and 4.0 right now */ if( p_output->i_physical_channels != AOUT_CHAN_CENTER && p_output->i_physical_channels != AOUT_CHANS_2_0 && - p_output->i_physical_channels != AOUT_CHANS_4_0 ) + p_output->i_physical_channels != AOUT_CHANS_4_0 && + p_output->i_physical_channels != AOUT_CHANS_5_1 ) { return false; }