output.c 14.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * output.c : internal management of output streams for the audio output
 *****************************************************************************
4
 * Copyright (C) 2002-2004 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
Antoine Cellerier's avatar
Antoine Cellerier 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>
Clément Stenac's avatar
Clément Stenac committed
32
#include <vlc_aout.h>
33 34 35 36
#include "aout_internal.h"

/*****************************************************************************
 * aout_OutputNew : allocate a new output and rework the filter pipeline
37
 *****************************************************************************
38
 * This function is entered with the mixer lock.
39 40 41 42
 *****************************************************************************/
int aout_OutputNew( aout_instance_t * p_aout,
                    audio_sample_format_t * p_format )
{
Christophe Massiot's avatar
Christophe Massiot committed
43
    /* Retrieve user defaults. */
44
    int i_rate = config_GetInt( p_aout, "aout-rate" );
Gildas Bazin's avatar
 
Gildas Bazin committed
45
    vlc_value_t val, text;
46 47
    /* kludge to avoid a fpu error when rate is 0... */
    if( i_rate == 0 ) i_rate = -1;
48 49

    memcpy( &p_aout->output.output, p_format, sizeof(audio_sample_format_t) );
50 51
    if ( i_rate != -1 )
        p_aout->output.output.i_rate = i_rate;
Christophe Massiot's avatar
Christophe Massiot committed
52
    aout_FormatPrepare( &p_aout->output.output );
53

54
    aout_lock_output_fifo( p_aout );
55

Christophe Massiot's avatar
Christophe Massiot committed
56
    /* Find the best output plug-in. */
Gildas Bazin's avatar
 
Gildas Bazin committed
57
    p_aout->output.p_module = module_Need( p_aout, "audio output", "$aout", 0);
Christophe Massiot's avatar
Christophe Massiot committed
58
    if ( p_aout->output.p_module == NULL )
59
    {
60
        msg_Err( p_aout, "no suitable audio output module" );
61
        aout_unlock_output_fifo( p_aout );
62 63
        return -1;
    }
64 65

    if ( var_Type( p_aout, "audio-channels" ) ==
Gildas Bazin's avatar
 
Gildas Bazin committed
66
             (VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
67 68 69 70
    {
        /* The user may have selected a different channels configuration. */
        var_Get( p_aout, "audio-channels", &val );

Gildas Bazin's avatar
 
Gildas Bazin committed
71
        if ( val.i_int == AOUT_VAR_CHAN_RSTEREO )
Christophe Massiot's avatar
Christophe Massiot committed
72 73 74 75
        {
            p_aout->output.output.i_original_channels |=
                                        AOUT_CHAN_REVERSESTEREO;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
76
        else if ( val.i_int == AOUT_VAR_CHAN_STEREO )
77 78 79 80
        {
            p_aout->output.output.i_original_channels =
                AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
81
        else if ( val.i_int == AOUT_VAR_CHAN_LEFT )
82 83 84
        {
            p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
85
        else if ( val.i_int == AOUT_VAR_CHAN_RIGHT )
86 87 88
        {
            p_aout->output.output.i_original_channels = AOUT_CHAN_RIGHT;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
89
        else if ( val.i_int == AOUT_VAR_CHAN_DOLBYS )
90 91 92 93 94
        {
            p_aout->output.output.i_original_channels
                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
        }
    }
95 96 97
    else if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER
              && (p_aout->output.output.i_original_channels
                   & AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
98 99
    {
        /* Mono - create the audio-channels variable. */
Gildas Bazin's avatar
 
Gildas Bazin committed
100 101
        var_Create( p_aout, "audio-channels",
                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
102
        text.psz_string = _("Audio Channels");
Gildas Bazin's avatar
 
Gildas Bazin committed
103 104 105 106 107 108 109 110
        var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );

        val.i_int = AOUT_VAR_CHAN_STEREO; text.psz_string = _("Stereo");
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
        val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
        val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
111 112 113 114
        if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
        {
            /* Go directly to the left channel. */
            p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
Gildas Bazin's avatar
 
Gildas Bazin committed
115
            val.i_int = AOUT_VAR_CHAN_LEFT;
116
            var_Set( p_aout, "audio-channels", val );
117 118 119 120
        }
        var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
                         NULL );
    }
121
    else if ( p_aout->output.output.i_physical_channels ==
122 123 124
               (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
                && (p_aout->output.output.i_original_channels &
                     (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
125 126
    {
        /* Stereo - create the audio-channels variable. */
Gildas Bazin's avatar
 
Gildas Bazin committed
127 128
        var_Create( p_aout, "audio-channels",
                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
129
        text.psz_string = _("Audio Channels");
Gildas Bazin's avatar
 
Gildas Bazin committed
130 131
        var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );

132 133
        if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
134 135
            val.i_int = AOUT_VAR_CHAN_DOLBYS;
            text.psz_string = _("Dolby Surround");
136 137 138
        }
        else
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
139 140
            val.i_int = AOUT_VAR_CHAN_STEREO;
            text.psz_string = _("Stereo");
141
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
142
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
Gildas Bazin's avatar
 
Gildas Bazin committed
143
        val.i_int = AOUT_VAR_CHAN_LEFT; text.psz_string = _("Left");
Gildas Bazin's avatar
 
Gildas Bazin committed
144 145 146 147 148
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
        val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
        val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
        var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
149 150 151 152
        if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
        {
            /* Go directly to the left channel. */
            p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
Gildas Bazin's avatar
 
Gildas Bazin committed
153
            val.i_int = AOUT_VAR_CHAN_LEFT;
154 155
            var_Set( p_aout, "audio-channels", val );
        }
156 157 158
        var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
                         NULL );
    }
159
    val.b_bool = true;
160
    var_Set( p_aout, "intf-change", val );
161

162
    aout_FormatPrepare( &p_aout->output.output );
163

164
    /* Prepare FIFO. */
165 166
    aout_FifoInit( p_aout, &p_aout->output.fifo,
                   p_aout->output.output.i_rate );
167

168
    aout_unlock_output_fifo( p_aout );
169

170
    aout_FormatPrint( p_aout, "output", &p_aout->output.output );
171 172

    /* Calculate the resulting mixer output format. */
173 174
    memcpy( &p_aout->mixer.mixer, &p_aout->output.output,
            sizeof(audio_sample_format_t) );
175
    if ( !AOUT_FMT_NON_LINEAR(&p_aout->output.output) )
176 177
    {
        /* Non-S/PDIF mixer only deals with float32 or fixed32. */
178
        p_aout->mixer.mixer.i_format
179
                     = (vlc_CPU() & CPU_CAPABILITY_FPU) ?
180 181
                        VLC_FOURCC('f','l','3','2') :
                        VLC_FOURCC('f','i','3','2');
182
        aout_FormatPrepare( &p_aout->mixer.mixer );
183
    }
184 185
    else
    {
186
        p_aout->mixer.mixer.i_format = p_format->i_format;
187 188
    }

189
    aout_FormatPrint( p_aout, "mixer", &p_aout->mixer.mixer );
190 191

    /* Create filters. */
192
    p_aout->output.i_nb_filters = 0;
193 194
    if ( aout_FiltersCreatePipeline( p_aout, p_aout->output.pp_filters,
                                     &p_aout->output.i_nb_filters,
195
                                     &p_aout->mixer.mixer,
196 197
                                     &p_aout->output.output ) < 0 )
    {
198
        msg_Err( p_aout, "couldn't create audio output pipeline" );
199 200 201 202 203 204 205
        module_Unneed( p_aout, p_aout->output.p_module );
        return -1;
    }

    /* Prepare hints for the buffer allocator. */
    p_aout->mixer.output_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
    p_aout->mixer.output_alloc.i_bytes_per_sec
206 207 208
                        = p_aout->mixer.mixer.i_bytes_per_frame
                           * p_aout->mixer.mixer.i_rate
                           / p_aout->mixer.mixer.i_frame_length;
209 210 211 212 213

    aout_FiltersHintBuffers( p_aout, p_aout->output.pp_filters,
                             p_aout->output.i_nb_filters,
                             &p_aout->mixer.output_alloc );

214
    p_aout->output.b_error = 0;
215 216 217 218 219
    return 0;
}

/*****************************************************************************
 * aout_OutputDelete : delete the output
220
 *****************************************************************************
221
 * This function is entered with the mixer lock.
222 223 224
 *****************************************************************************/
void aout_OutputDelete( aout_instance_t * p_aout )
{
225 226 227 228 229
    if ( p_aout->output.b_error )
    {
        return;
    }

230 231 232 233
    module_Unneed( p_aout, p_aout->output.p_module );

    aout_FiltersDestroyPipeline( p_aout, p_aout->output.pp_filters,
                                 p_aout->output.i_nb_filters );
234

235
    aout_lock_output_fifo( p_aout );
236
    aout_FifoDestroy( p_aout, &p_aout->output.fifo );
237
    aout_unlock_output_fifo( p_aout );
238

239
    p_aout->output.b_error = true;
240 241 242 243
}

/*****************************************************************************
 * aout_OutputPlay : play a buffer
244
 *****************************************************************************
245
 * This function is entered with the mixer lock.
246 247 248 249 250 251 252
 *****************************************************************************/
void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
{
    aout_FiltersPlay( p_aout, p_aout->output.pp_filters,
                      p_aout->output.i_nb_filters,
                      &p_buffer );

253 254 255 256 257 258
    if( p_buffer->i_nb_bytes == 0 )
    {
        aout_BufferFree( p_buffer );
        return;
    }

259
    aout_lock_output_fifo( p_aout );
260 261
    aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
    p_aout->output.pf_play( p_aout );
262
    aout_unlock_output_fifo( p_aout );
263 264 265 266
}

/*****************************************************************************
 * aout_OutputNextBuffer : give the audio output plug-in the right buffer
267 268 269
 *****************************************************************************
 * If b_can_sleek is 1, the aout core functions won't try to resample
 * new buffers to catch up - that is we suppose that the output plug-in can
270
 * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
271
 * This function is entered with no lock at all :-).
272 273
 *****************************************************************************/
aout_buffer_t * aout_OutputNextBuffer( aout_instance_t * p_aout,
274
                                       mtime_t start_date,
275
                                       bool b_can_sleek )
276 277 278
{
    aout_buffer_t * p_buffer;

279
    aout_lock_output_fifo( p_aout );
280

281
    p_buffer = p_aout->output.fifo.p_first;
282 283 284 285 286 287

    /* Drop the audio sample if the audio output is really late.
     * In the case of b_can_sleek, we don't use a resampler so we need to be
     * a lot more severe. */
    while ( p_buffer && p_buffer->start_date <
            (b_can_sleek ? start_date : mdate()) - AOUT_PTS_TOLERANCE )
288
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
289 290
        msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
                 "trashing %"PRId64"us", mdate() - p_buffer->start_date,
291
                 p_buffer->end_date - p_buffer->start_date );
292
        p_buffer = p_buffer->p_next;
Gildas Bazin's avatar
 
Gildas Bazin committed
293 294
        aout_BufferFree( p_aout->output.fifo.p_first );
        p_aout->output.fifo.p_first = p_buffer;
295 296 297 298 299
    }

    if ( p_buffer == NULL )
    {
        p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
Gildas Bazin's avatar
 
Gildas Bazin committed
300 301

#if 0 /* This is bad because the audio output might just be trying to fill
302
       * in its internal buffers. And anyway, it's up to the audio output
Gildas Bazin's avatar
 
Gildas Bazin committed
303 304
       * to deal with this kind of starvation. */

305
        /* Set date to 0, to allow the mixer to send a new buffer ASAP */
306
        aout_FifoSet( p_aout, &p_aout->output.fifo, 0 );
307 308
        if ( !p_aout->output.b_starving )
            msg_Dbg( p_aout,
309
                 "audio output is starving (no input), playing silence" );
310
        p_aout->output.b_starving = 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
311 312
#endif

313
        aout_unlock_output_fifo( p_aout );
314 315 316
        return NULL;
    }

317
    /* Here we suppose that all buffers have the same duration - this is
318 319
     * generally true, and anyway if it's wrong it won't be a disaster.
     */
320
    if ( p_buffer->start_date > start_date
Gildas Bazin's avatar
 
Gildas Bazin committed
321 322 323 324 325 326 327
                         + (p_buffer->end_date - p_buffer->start_date) )
    /*
     *                   + AOUT_PTS_TOLERANCE )
     * There is no reason to want that, it just worsen the scheduling of
     * an audio sample after an output starvation (ie. on start or on resume)
     * --Gibalou
     */
328
    {
329
        const mtime_t i_delta = p_buffer->start_date - start_date;
330
        aout_unlock_output_fifo( p_aout );
331

332
        if ( !p_aout->output.b_starving )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
333
            msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
334
                     "playing silence", i_delta );
335
        p_aout->output.b_starving = 1;
336 337 338
        return NULL;
    }

339 340
    p_aout->output.b_starving = 0;

341
    if ( !b_can_sleek &&
342 343
          ( (p_buffer->start_date - start_date > AOUT_PTS_TOLERANCE)
             || (start_date - p_buffer->start_date > AOUT_PTS_TOLERANCE) ) )
344 345 346
    {
        /* Try to compensate the drift by doing some resampling. */
        int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
347 348
        mtime_t difference = start_date - p_buffer->start_date;
        msg_Warn( p_aout, "output date isn't PTS date, requesting "
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
349
                  "resampling (%"PRId64")", difference );
350

351
        aout_lock_input_fifos( p_aout );
352
        for ( i = 0; i < p_aout->i_nb_inputs; i++ )
353
        {
354 355 356
            aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;

            aout_FifoMoveDates( p_aout, p_fifo, difference );
357
        }
358 359

        aout_FifoMoveDates( p_aout, &p_aout->output.fifo, difference );
360
        aout_unlock_input_fifos( p_aout );
361
    }
362 363 364 365 366 367 368

    p_aout->output.fifo.p_first = p_buffer->p_next;
    if ( p_buffer->p_next == NULL )
    {
        p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
    }

369
    aout_unlock_output_fifo( p_aout );
370 371
    return p_buffer;
}