filters.c 12.4 KB
Newer Older
1 2 3
/*****************************************************************************
 * filters.c : audio output filters management
 *****************************************************************************
Clément Stenac's avatar
Clément Stenac committed
4
 * Copyright (C) 2002-2004 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: filters.c,v 1.20 2004/03/03 20:39:52 gbazin Exp $
6 7 8 9 10 11 12
 *
 * 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.
13
 *
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * 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 <stdlib.h>                            /* calloc(), malloc(), free() */
#include <string.h>
29 30 31

#include <vlc/vlc.h>

32
#ifdef HAVE_ALLOCA_H
33
#   include <alloca.h>
34 35 36 37 38
#endif

#include "audio_output.h"
#include "aout_internal.h"

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*****************************************************************************
 * FindFilter: find an audio filter for a specific transformation
 *****************************************************************************/
static aout_filter_t * FindFilter( aout_instance_t * p_aout,
                             const audio_sample_format_t * p_input_format,
                             const audio_sample_format_t * p_output_format )
{
    aout_filter_t * p_filter = vlc_object_create( p_aout,
                                                  sizeof(aout_filter_t) );

    if ( p_filter == NULL ) return NULL;
    vlc_object_attach( p_filter, p_aout );

    memcpy( &p_filter->input, p_input_format, sizeof(audio_sample_format_t) );
    memcpy( &p_filter->output, p_output_format,
            sizeof(audio_sample_format_t) );
Gildas Bazin's avatar
 
Gildas Bazin committed
55
    p_filter->p_module = module_Need( p_filter, "audio filter", NULL, 0 );
56 57 58 59 60 61 62
    if ( p_filter->p_module == NULL )
    {
        vlc_object_detach( p_filter );
        vlc_object_destroy( p_filter );
        return NULL;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
63
    p_filter->b_continuity = VLC_FALSE;
Gildas Bazin's avatar
 
Gildas Bazin committed
64

65 66 67 68
    return p_filter;
}

/*****************************************************************************
69
 * SplitConversion: split a conversion in two parts
70 71 72 73 74 75 76
 *****************************************************************************
 * Returns the number of conversions required by the first part - 0 if only
 * one conversion was asked.
 * Beware : p_output_format can be modified during this function if the
 * developer passed SplitConversion( toto, titi, titi, ... ). That is legal.
 * SplitConversion( toto, titi, toto, ... ) isn't.
 *****************************************************************************/
77 78 79
static int SplitConversion( const audio_sample_format_t * p_input_format,
                            const audio_sample_format_t * p_output_format,
                            audio_sample_format_t * p_middle_format )
80
{
Christophe Massiot's avatar
Christophe Massiot committed
81
    vlc_bool_t b_format =
82
             (p_input_format->i_format != p_output_format->i_format);
Christophe Massiot's avatar
Christophe Massiot committed
83 84
    vlc_bool_t b_rate = (p_input_format->i_rate != p_output_format->i_rate);
    vlc_bool_t b_channels =
85 86 87 88
        (p_input_format->i_physical_channels
          != p_output_format->i_physical_channels)
     || (p_input_format->i_original_channels
          != p_output_format->i_original_channels);
89 90 91 92 93 94 95 96
    int i_nb_conversions = b_format + b_rate + b_channels;

    if ( i_nb_conversions <= 1 ) return 0;

    memcpy( p_middle_format, p_output_format, sizeof(audio_sample_format_t) );

    if ( i_nb_conversions == 2 )
    {
97
        if ( !b_format || !b_channels )
98
        {
99
            p_middle_format->i_rate = p_input_format->i_rate;
100
            aout_FormatPrepare( p_middle_format );
101 102 103
            return 1;
        }

104
        /* !b_rate */
105 106 107 108
        p_middle_format->i_physical_channels
             = p_input_format->i_physical_channels;
        p_middle_format->i_original_channels
             = p_input_format->i_original_channels;
109
        aout_FormatPrepare( p_middle_format );
110 111 112 113
        return 1;
    }

    /* i_nb_conversion == 3 */
114
    p_middle_format->i_rate = p_input_format->i_rate;
115
    aout_FormatPrepare( p_middle_format );
116 117 118
    return 2;
}

119 120 121 122 123 124 125 126 127
/*****************************************************************************
 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
 *                             format to another
 *****************************************************************************
 * TODO : allow the user to add/remove specific filters
 *****************************************************************************/
int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
                                aout_filter_t ** pp_filters,
                                int * pi_nb_filters,
128 129
                                const audio_sample_format_t * p_input_format,
                                const audio_sample_format_t * p_output_format )
130
{
131
    audio_sample_format_t temp_format;
132
    int i_nb_conversions;
133

134 135 136 137 138 139 140
    if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
    {
        msg_Dbg( p_aout, "no need for any filter" );
        *pi_nb_filters = 0;
        return 0;
    }

141
    aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
142 143

    /* Try to find a filter to do the whole conversion. */
144 145
    pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
    if ( pp_filters[0] != NULL )
146 147 148 149 150 151
    {
        msg_Dbg( p_aout, "found a filter for the whole conversion" );
        *pi_nb_filters = 1;
        return 0;
    }

152
    /* We'll have to split the conversion. We always do the downmixing
153 154
     * before the resampling, because the audio decoder can probably do it
     * for us. */
155
    i_nb_conversions = SplitConversion( p_input_format,
156 157 158 159 160 161 162
                                        p_output_format, &temp_format );
    if ( !i_nb_conversions )
    {
        /* There was only one conversion to do, and we already failed. */
        msg_Err( p_aout, "couldn't find a filter for the conversion" );
        return -1;
    }
163

164 165
    pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
    if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
166
    {
167
        /* Try with only one conversion. */
168 169
        SplitConversion( p_input_format, &temp_format, &temp_format );
        pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
170 171 172 173 174 175 176
    }
    if ( pp_filters[0] == NULL )
    {
        msg_Err( p_aout,
              "couldn't find a filter for the first part of the conversion" );
        return -1;
    }
177

178 179 180 181 182 183 184
    /* We have the first stage of the conversion. Find a filter for
     * the rest. */
    pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
                                p_output_format );
    if ( pp_filters[1] == NULL )
    {
        /* Try to split the conversion. */
185 186
        i_nb_conversions = SplitConversion( &pp_filters[0]->output,
                                           p_output_format, &temp_format );
187
        if ( !i_nb_conversions )
188
        {
189 190 191 192
            vlc_object_detach( pp_filters[0] );
            vlc_object_destroy( pp_filters[0] );
            msg_Err( p_aout,
              "couldn't find a filter for the second part of the conversion" );
193 194
        }
        pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
195 196
                                    &temp_format );
        pp_filters[2] = FindFilter( p_aout, &temp_format,
197
                                    p_output_format );
198 199

        if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
200
        {
201 202 203
            vlc_object_detach( pp_filters[0] );
            vlc_object_destroy( pp_filters[0] );
            if ( pp_filters[1] != NULL )
204
            {
205 206
                vlc_object_detach( pp_filters[1] );
                vlc_object_destroy( pp_filters[1] );
207
            }
208
            if ( pp_filters[2] != NULL )
209
            {
210 211
                vlc_object_detach( pp_filters[2] );
                vlc_object_destroy( pp_filters[2] );
212
            }
213 214
            msg_Err( p_aout,
               "couldn't find filters for the second part of the conversion" );
215
        }
216 217 218 219 220
        *pi_nb_filters = 3;
    }
    else
    {
        *pi_nb_filters = 2;
221
    }
222

223 224 225 226
    /* We have enough filters. */
    msg_Dbg( p_aout, "found %d filters for the whole conversion",
             *pi_nb_filters );
    return 0;
227 228 229 230 231 232 233 234 235 236 237 238 239 240
}

/*****************************************************************************
 * aout_FiltersDestroyPipeline: deallocate a filters pipeline
 *****************************************************************************/
void aout_FiltersDestroyPipeline( aout_instance_t * p_aout,
                                  aout_filter_t ** pp_filters,
                                  int i_nb_filters )
{
    int i;

    for ( i = 0; i < i_nb_filters; i++ )
    {
        module_Unneed( pp_filters[i], pp_filters[i]->p_module );
241
        vlc_object_detach( pp_filters[i] );
242 243 244 245 246 247 248 249 250 251 252 253 254 255
        vlc_object_destroy( pp_filters[i] );
    }
}

/*****************************************************************************
 * aout_FiltersHintBuffers: fill in aout_alloc_t structures to optimize
 *                          buffer allocations
 *****************************************************************************/
void aout_FiltersHintBuffers( aout_instance_t * p_aout,
                              aout_filter_t ** pp_filters,
                              int i_nb_filters, aout_alloc_t * p_first_alloc )
{
    int i;

256 257
    (void)p_aout; /* unused */

258 259 260 261
    for ( i = i_nb_filters - 1; i >= 0; i-- )
    {
        aout_filter_t * p_filter = pp_filters[i];

262 263 264 265 266 267
        int i_output_size = p_filter->output.i_bytes_per_frame
                             * p_filter->output.i_rate
                             / p_filter->output.i_frame_length;
        int i_input_size = p_filter->input.i_bytes_per_frame
                             * p_filter->input.i_rate
                             / p_filter->input.i_frame_length;
268 269 270 271 272 273 274 275 276

        p_first_alloc->i_bytes_per_sec = __MAX( p_first_alloc->i_bytes_per_sec,
                                                i_output_size );

        if ( p_filter->b_in_place )
        {
            p_first_alloc->i_bytes_per_sec = __MAX(
                                         p_first_alloc->i_bytes_per_sec,
                                         i_input_size );
277
            p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
        }
        else
        {
            /* We're gonna need a buffer allocation. */
            memcpy( &p_filter->output_alloc, p_first_alloc,
                    sizeof(aout_alloc_t) );
            p_first_alloc->i_alloc_type = AOUT_ALLOC_STACK;
            p_first_alloc->i_bytes_per_sec = i_input_size;
        }
    }
}

/*****************************************************************************
 * aout_FiltersPlay: play a buffer
 *****************************************************************************/
void aout_FiltersPlay( aout_instance_t * p_aout,
                       aout_filter_t ** pp_filters,
                       int i_nb_filters, aout_buffer_t ** pp_input_buffer )
{
    int i;

    for ( i = 0; i < i_nb_filters; i++ )
    {
        aout_filter_t * p_filter = pp_filters[i];
        aout_buffer_t * p_output_buffer;

Gildas Bazin's avatar
 
Gildas Bazin committed
304 305 306
        /* Resamplers can produce slightly more samples than (i_in_nb *
         * p_filter->output.i_rate / p_filter->input.i_rate) so we need
         * slightly bigger buffers. */
307
        aout_BufferAlloc( &p_filter->output_alloc,
Gildas Bazin's avatar
 
Gildas Bazin committed
308
            ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
Gildas Bazin's avatar
 
Gildas Bazin committed
309 310
            * 1000000 / p_filter->input.i_rate,
            *pp_input_buffer, p_output_buffer );
311 312 313 314 315
        if ( p_output_buffer == NULL )
        {
            msg_Err( p_aout, "out of memory" );
            return;
        }
316 317
        /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
         * shall be set by the filter plug-in. */
318 319 320 321 322 323 324 325 326 327 328 329

        p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
                              p_output_buffer );

        if ( !p_filter->b_in_place )
        {
            aout_BufferFree( *pp_input_buffer );
            *pp_input_buffer = p_output_buffer;
        }
    }
}