filters.c 14.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * filters.c : audio output filters management
 *****************************************************************************
4
 * Copyright (C) 2002-2007 the VideoLAN team
5
 * $Id$
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
 * 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
dionoea's avatar
dionoea committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
zorglub's avatar
zorglub committed
32
#include <vlc_interface.h>
33

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

zorglub's avatar
zorglub committed
38
#include <vlc_aout.h>
39
#include "aout_internal.h"
40
#include <libvlc.h>
41

42 43 44 45 46 47 48
/*****************************************************************************
 * 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 )
{
49 50 51 52 53
    static const char typename[] = "audio output";
    aout_filter_t * p_filter;

    p_filter = vlc_custom_create( p_aout, sizeof(*p_filter),
                                  VLC_OBJECT_GENERIC, typename );
54 55 56 57 58 59 60

    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) );
61
    p_filter->p_module = module_need( p_filter, "audio filter", NULL, false );
62 63 64
    if ( p_filter->p_module == NULL )
    {
        vlc_object_detach( p_filter );
65
        vlc_object_release( p_filter );
66 67 68
        return NULL;
    }

69
    p_filter->b_continuity = false;
gbazin's avatar
 
gbazin committed
70

71 72 73 74
    return p_filter;
}

/*****************************************************************************
75
 * SplitConversion: split a conversion in two parts
76 77 78 79 80 81 82
 *****************************************************************************
 * 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.
 *****************************************************************************/
83 84 85
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 )
86
{
87
    bool b_format =
88
             (p_input_format->i_format != p_output_format->i_format);
89 90
    bool b_rate = (p_input_format->i_rate != p_output_format->i_rate);
    bool b_channels =
91 92 93 94
        (p_input_format->i_physical_channels
          != p_output_format->i_physical_channels)
     || (p_input_format->i_original_channels
          != p_output_format->i_original_channels);
95 96 97 98 99 100 101 102
    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 )
    {
103
        if ( !b_format || !b_channels )
104
        {
105
            p_middle_format->i_rate = p_input_format->i_rate;
106
            aout_FormatPrepare( p_middle_format );
107 108 109
            return 1;
        }

110
        /* !b_rate */
111 112 113 114
        p_middle_format->i_physical_channels
             = p_input_format->i_physical_channels;
        p_middle_format->i_original_channels
             = p_input_format->i_original_channels;
115
        aout_FormatPrepare( p_middle_format );
116 117 118 119
        return 1;
    }

    /* i_nb_conversion == 3 */
120
    p_middle_format->i_rate = p_input_format->i_rate;
121
    aout_FormatPrepare( p_middle_format );
122 123 124
    return 2;
}

125 126
static void ReleaseFilter( aout_filter_t * p_filter )
{
127
    module_unneed( p_filter, p_filter->p_module );
128
    vlc_object_detach( p_filter );
129
    vlc_object_release( p_filter );
130 131
}

132 133 134 135
/*****************************************************************************
 * aout_FiltersCreatePipeline: create a filters pipeline to transform a sample
 *                             format to another
 *****************************************************************************
136
 * pi_nb_filters must be initialized before calling this function
137 138
 *****************************************************************************/
int aout_FiltersCreatePipeline( aout_instance_t * p_aout,
139
                                aout_filter_t ** pp_filters_start,
140
                                int * pi_nb_filters,
141 142
                                const audio_sample_format_t * p_input_format,
                                const audio_sample_format_t * p_output_format )
143
{
144
    aout_filter_t** pp_filters = pp_filters_start + *pi_nb_filters;
145
    audio_sample_format_t temp_format;
146
    int i_nb_conversions;
147

148 149 150 151 152 153
    if ( AOUT_FMTS_IDENTICAL( p_input_format, p_output_format ) )
    {
        msg_Dbg( p_aout, "no need for any filter" );
        return 0;
    }

154
    aout_FormatsPrint( p_aout, "filter(s)", p_input_format, p_output_format );
155

156 157 158
    if( *pi_nb_filters + 1 > AOUT_MAX_FILTERS )
    {
        msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
159
        intf_UserFatal( p_aout, false, _("Audio filtering failed"),
160 161
                        _("The maximum number of filters (%d) was reached."),
                        AOUT_MAX_FILTERS );
162 163 164
        return -1;
    }

165
    /* Try to find a filter to do the whole conversion. */
166 167
    pp_filters[0] = FindFilter( p_aout, p_input_format, p_output_format );
    if ( pp_filters[0] != NULL )
168 169
    {
        msg_Dbg( p_aout, "found a filter for the whole conversion" );
170
        ++*pi_nb_filters;
171 172 173
        return 0;
    }

174
    /* We'll have to split the conversion. We always do the downmixing
175 176
     * before the resampling, because the audio decoder can probably do it
     * for us. */
177
    i_nb_conversions = SplitConversion( p_input_format,
178 179 180 181 182 183 184
                                        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;
    }
185

186 187
    pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
    if ( pp_filters[0] == NULL && i_nb_conversions == 2 )
188
    {
189
        /* Try with only one conversion. */
190 191
        SplitConversion( p_input_format, &temp_format, &temp_format );
        pp_filters[0] = FindFilter( p_aout, p_input_format, &temp_format );
192 193 194 195 196 197 198
    }
    if ( pp_filters[0] == NULL )
    {
        msg_Err( p_aout,
              "couldn't find a filter for the first part of the conversion" );
        return -1;
    }
199

200 201
    /* We have the first stage of the conversion. Find a filter for
     * the rest. */
202 203 204 205
    if( *pi_nb_filters + 2 > AOUT_MAX_FILTERS )
    {
        ReleaseFilter( pp_filters[0] );
        msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
206
        intf_UserFatal( p_aout, false, _("Audio filtering failed"),
207 208
                        _("The maximum number of filters (%d) was reached."),
                        AOUT_MAX_FILTERS );
209 210
        return -1;
    }
211 212 213 214 215
    pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
                                p_output_format );
    if ( pp_filters[1] == NULL )
    {
        /* Try to split the conversion. */
216 217
        i_nb_conversions = SplitConversion( &pp_filters[0]->output,
                                           p_output_format, &temp_format );
218
        if ( !i_nb_conversions )
219
        {
220
            ReleaseFilter( pp_filters[0] );
221 222
            msg_Err( p_aout,
              "couldn't find a filter for the second part of the conversion" );
223 224 225 226 227 228
            return -1;
        }
        if( *pi_nb_filters + 3 > AOUT_MAX_FILTERS )
        {
            ReleaseFilter( pp_filters[0] );
            msg_Err( p_aout, "max filter reached (%d)", AOUT_MAX_FILTERS );
229
            intf_UserFatal( p_aout, false, _("Audio filtering failed"),
230 231
                            _("The maximum number of filters (%d) was reached."),
                            AOUT_MAX_FILTERS );
232
            return -1;
233 234
        }
        pp_filters[1] = FindFilter( p_aout, &pp_filters[0]->output,
235 236
                                    &temp_format );
        pp_filters[2] = FindFilter( p_aout, &temp_format,
237
                                    p_output_format );
238 239

        if ( pp_filters[1] == NULL || pp_filters[2] == NULL )
240
        {
241
            ReleaseFilter( pp_filters[0] );
242
            if ( pp_filters[1] != NULL )
243
            {
244
                ReleaseFilter( pp_filters[1] );
245
            }
246
            if ( pp_filters[2] != NULL )
247
            {
248
                ReleaseFilter( pp_filters[2] );
249
            }
250 251
            msg_Err( p_aout,
               "couldn't find filters for the second part of the conversion" );
252
            return -1;
253
        }
254 255
        *pi_nb_filters += 3;
        msg_Dbg( p_aout, "found 3 filters for the whole conversion" );
256 257 258
    }
    else
    {
259 260
        *pi_nb_filters += 2;
        msg_Dbg( p_aout, "found 2 filters for the whole conversion" );
261
    }
262

263
    return 0;
264 265 266 267 268 269 270 271 272 273
}

/*****************************************************************************
 * 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;
274
    (void)p_aout;
275 276 277

    for ( i = 0; i < i_nb_filters; i++ )
    {
278 279 280 281 282 283
        aout_filter_t *p_filter = pp_filters[i];

        module_unneed( p_filter, p_filter->p_module );
        free( p_filter->p_owner );
        vlc_object_detach( p_filter );
        vlc_object_release( p_filter );
284 285 286 287 288 289 290 291 292 293 294 295 296
    }
}

/*****************************************************************************
 * 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;

297 298
    (void)p_aout; /* unused */

299 300 301 302
    for ( i = i_nb_filters - 1; i >= 0; i-- )
    {
        aout_filter_t * p_filter = pp_filters[i];

303
        int i_output_size = p_filter->output.i_bytes_per_frame
304
                             * p_filter->output.i_rate * AOUT_MAX_INPUT_RATE
305 306
                             / p_filter->output.i_frame_length;
        int i_input_size = p_filter->input.i_bytes_per_frame
307
                             * p_filter->input.i_rate * AOUT_MAX_INPUT_RATE
308
                             / p_filter->input.i_frame_length;
309 310 311 312 313 314 315 316 317

        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 );
318
            p_filter->output_alloc.i_alloc_type = AOUT_ALLOC_NONE;
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        }
        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;

340
    for( i = 0; i < i_nb_filters; i++ )
341 342 343 344
    {
        aout_filter_t * p_filter = pp_filters[i];
        aout_buffer_t * p_output_buffer;

gbazin's avatar
 
gbazin committed
345 346 347
        /* 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. */
348
        aout_BufferAlloc( &p_filter->output_alloc,
349 350 351 352
                          ((mtime_t)(*pp_input_buffer)->i_nb_samples + 2)
                          * 1000000 / p_filter->input.i_rate,
                          *pp_input_buffer, p_output_buffer );
        if( p_output_buffer == NULL )
353
            return;
354

355 356
        /* Please note that p_output_buffer->i_nb_samples & i_nb_bytes
         * shall be set by the filter plug-in. */
357 358 359 360 361 362 363 364 365 366
        if( (*pp_input_buffer)->i_nb_samples > 0 )
        {
            p_filter->pf_do_work( p_aout, p_filter, *pp_input_buffer,
                                  p_output_buffer );
        }
        else
        {
            p_output_buffer->i_nb_bytes = 0;
            p_output_buffer->i_nb_samples = 0;
        }
367

368
        if( !p_filter->b_in_place )
369 370 371 372 373
        {
            aout_BufferFree( *pp_input_buffer );
            *pp_input_buffer = p_output_buffer;
        }
    }
374 375

    assert( (*pp_input_buffer) == NULL || (*pp_input_buffer)->i_alloc_type != AOUT_ALLOC_STACK );
376 377
}

378 379 380 381 382 383
/*****************************************************************************
 * aout_filter_RequestVout
 *****************************************************************************/
vout_thread_t *aout_filter_RequestVout( aout_filter_t *p_filter,
                                        vout_thread_t *p_vout, video_format_t *p_fmt )
{
384
    if( !p_filter->request_vout.pf_request_vout )
385
        return NULL;
386
    return p_filter->request_vout.pf_request_vout( p_filter->request_vout.p_private, p_vout, p_fmt );
387 388
}