dec.c 11.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * dec.c : audio output API towards decoders
 *****************************************************************************
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
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 32
#include <assert.h>

33
#include <vlc_common.h>
34 35 36 37 38

#ifdef HAVE_ALLOCA_H
#   include <alloca.h>
#endif

Clément Stenac's avatar
Clément Stenac committed
39 40 41
#include <vlc_aout.h>
#include <vlc_input.h>

42 43 44 45 46
#include "aout_internal.h"

/*****************************************************************************
 * aout_DecNew : create a decoder
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
47
static aout_input_t * DecNew( aout_instance_t * p_aout,
48
                              audio_sample_format_t *p_format,
49 50
                              const audio_replay_gain_t *p_replay_gain,
                              const aout_request_vout_t *p_request_vout )
51 52 53
{
    aout_input_t * p_input;

54 55 56 57 58
    /* Sanitize audio format */
    if( p_format->i_channels > 32 )
    {
        msg_Err( p_aout, "too many audio channels (%u)",
                 p_format->i_channels );
59
        return NULL;
60
    }
61 62 63 64 65
    if( p_format->i_channels <= 0 )
    {
        msg_Err( p_aout, "no audio channels" );
        return NULL;
    }
66 67 68 69 70
    if( p_format->i_channels != aout_FormatNbChannels( p_format ) )
    {
        msg_Err( p_aout, "incompatible audio channels count with layout mask" );
        return NULL;
    }
71 72 73 74 75

    if( p_format->i_rate > 192000 )
    {
        msg_Err( p_aout, "excessive audio sample frequency (%u)",
                 p_format->i_rate );
76
        return NULL;
77
    }
78 79 80 81 82 83
    if( p_format->i_rate < 4000 )
    {
        msg_Err( p_aout, "too low audio sample frequency (%u)",
                 p_format->i_rate );
        return NULL;
    }
84

85 86
    /* We can only be called by the decoder, so no need to lock
     * p_input->lock. */
87
    aout_lock_mixer( p_aout );
88 89 90 91

    if ( p_aout->i_nb_inputs >= AOUT_MAX_INPUTS )
    {
        msg_Err( p_aout, "too many inputs already (%d)", p_aout->i_nb_inputs );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
92
        goto error;
93 94
    }

95 96
    p_input = calloc( 1, sizeof(aout_input_t));
    if( !p_input )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
97
        goto error;
98

99
    vlc_mutex_init( &p_input->lock );
100

Laurent Aimar's avatar
Laurent Aimar committed
101 102 103 104
    p_input->b_changed = false;
    p_input->b_error = true;
    p_input->b_paused = false;
    p_input->i_pause_date = 0;
105

106
    aout_FormatPrepare( p_format );
107

108 109
    memcpy( &p_input->input, p_format,
            sizeof(audio_sample_format_t) );
110 111
    if( p_replay_gain )
        p_input->replay_gain = *p_replay_gain;
112

113
    aout_lock_input_fifos( p_aout );
114 115 116
    p_aout->pp_inputs[p_aout->i_nb_inputs] = p_input;
    p_aout->i_nb_inputs++;

117
    if ( !p_aout->p_mixer )
118 119 120
    {
        int i;

Gildas Bazin's avatar
 
Gildas Bazin committed
121 122
        var_Destroy( p_aout, "audio-device" );
        var_Destroy( p_aout, "audio-channels" );
123

124 125 126 127 128
        /* Recreate the output using the new format. */
        if ( aout_OutputNew( p_aout, p_format ) < 0 )
        {
            for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
            {
129
                aout_lock_input( p_aout, p_aout->pp_inputs[i] );
130
                aout_InputDelete( p_aout, p_aout->pp_inputs[i] );
131
                aout_unlock_input( p_aout, p_aout->pp_inputs[i] );
132
            }
133 134
            aout_unlock_input_fifos( p_aout );
            aout_unlock_mixer( p_aout );
135 136 137 138 139 140
            return p_input;
        }

        /* Create other input streams. */
        for ( i = 0; i < p_aout->i_nb_inputs - 1; i++ )
        {
141 142 143 144 145 146
            aout_input_t *p_input = p_aout->pp_inputs[i];

            aout_lock_input( p_aout, p_input );
            aout_InputDelete( p_aout, p_input );
            aout_InputNew( p_aout, p_input, &p_input->request_vout );
            aout_unlock_input( p_aout, p_input );
147 148 149 150 151 152 153 154 155 156
        }
    }
    else
    {
        aout_MixerDelete( p_aout );
    }

    if ( aout_MixerNew( p_aout ) == -1 )
    {
        aout_OutputDelete( p_aout );
157
        aout_unlock_input_fifos( p_aout );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
158
        goto error;
159 160
    }

161
    aout_InputNew( p_aout, p_input, p_request_vout );
162
    aout_unlock_input_fifos( p_aout );
163

164
    aout_unlock_mixer( p_aout );
Gildas Bazin's avatar
 
Gildas Bazin committed
165

166
    return p_input;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
167 168

error:
169
    aout_unlock_mixer( p_aout );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
170
    return NULL;
171 172 173 174
}

aout_input_t * __aout_DecNew( vlc_object_t * p_this,
                              aout_instance_t ** pp_aout,
175
                              audio_sample_format_t * p_format,
176 177
                              const audio_replay_gain_t *p_replay_gain,
                              const aout_request_vout_t *p_request_video )
178
{
179 180
    aout_instance_t *p_aout = *pp_aout;
    if ( p_aout == NULL )
181
    {
182 183
        msg_Dbg( p_this, "no aout present, spawning one" );
        p_aout = aout_New( p_this );
184

185 186 187
        /* Everything failed, I'm a loser, I just wanna die */
        if( p_aout == NULL )
            return NULL;
188

189 190
        vlc_object_attach( p_aout, p_this );
        *pp_aout = p_aout;
191 192
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
193
    return DecNew( p_aout, p_format, p_replay_gain, p_request_video );
194 195 196 197 198 199 200 201 202 203 204
}

/*****************************************************************************
 * aout_DecDelete : delete a decoder
 *****************************************************************************/
int aout_DecDelete( aout_instance_t * p_aout, aout_input_t * p_input )
{
    int i_input;

    /* This function can only be called by the decoder itself, so no need
     * to lock p_input->lock. */
205
    aout_lock_mixer( p_aout );
206 207 208 209 210 211 212 213 214 215 216 217

    for ( i_input = 0; i_input < p_aout->i_nb_inputs; i_input++ )
    {
        if ( p_aout->pp_inputs[i_input] == p_input )
        {
            break;
        }
    }

    if ( i_input == p_aout->i_nb_inputs )
    {
        msg_Err( p_aout, "cannot find an input to delete" );
218
        aout_unlock_mixer( p_aout );
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        return -1;
    }

    /* Remove the input from the list. */
    memmove( &p_aout->pp_inputs[i_input], &p_aout->pp_inputs[i_input + 1],
             (AOUT_MAX_INPUTS - i_input - 1) * sizeof(aout_input_t *) );
    p_aout->i_nb_inputs--;

    aout_InputDelete( p_aout, p_input );

    vlc_mutex_destroy( &p_input->lock );
    free( p_input );

    if ( !p_aout->i_nb_inputs )
    {
        aout_OutputDelete( p_aout );
        aout_MixerDelete( p_aout );
236 237
        var_Destroy( p_aout, "audio-device" );
        var_Destroy( p_aout, "audio-channels" );
238 239
    }

240
    aout_unlock_mixer( p_aout );
241 242 243 244 245 246 247 248 249 250 251 252

    return 0;
}


/*
 * Buffer management
 */

/*****************************************************************************
 * aout_DecNewBuffer : ask for a new empty buffer
 *****************************************************************************/
253
aout_buffer_t * aout_DecNewBuffer( aout_input_t * p_input,
254 255 256 257 258
                                   size_t i_nb_samples )
{
    aout_buffer_t * p_buffer;
    mtime_t duration;

259
    aout_lock_input( NULL, p_input );
260 261 262

    if ( p_input->b_error )
    {
263
        aout_unlock_input( NULL, p_input );
264 265 266 267 268 269
        return NULL;
    }

    duration = (1000000 * (mtime_t)i_nb_samples) / p_input->input.i_rate;

    /* This necessarily allocates in the heap. */
270
    p_buffer = aout_BufferAlloc( &p_input->input_alloc, duration, NULL );
271 272 273
    if( p_buffer != NULL )
        p_buffer->i_nb_bytes = i_nb_samples * p_input->input.i_bytes_per_frame
                                  / p_input->input.i_frame_length;
274 275

    /* Suppose the decoder doesn't have more than one buffered buffer */
Laurent Aimar's avatar
Laurent Aimar committed
276
    p_input->b_changed = false;
277

278
    aout_unlock_input( NULL, p_input );
279

280 281
    if( p_buffer == NULL )
        return NULL;
282

283
    p_buffer->i_nb_samples = i_nb_samples;
284
    p_buffer->i_pts = p_buffer->i_length = 0;
285 286 287 288 289 290 291 292 293
    return p_buffer;
}

/*****************************************************************************
 * aout_DecDeleteBuffer : destroy an undecoded buffer
 *****************************************************************************/
void aout_DecDeleteBuffer( aout_instance_t * p_aout, aout_input_t * p_input,
                           aout_buffer_t * p_buffer )
{
294
    (void)p_aout; (void)p_input;
295 296 297 298 299 300 301
    aout_BufferFree( p_buffer );
}

/*****************************************************************************
 * aout_DecPlay : filter & mix the decoded buffer
 *****************************************************************************/
int aout_DecPlay( aout_instance_t * p_aout, aout_input_t * p_input,
302
                  aout_buffer_t * p_buffer, int i_input_rate )
303
{
304 305
    assert( i_input_rate >= INPUT_RATE_DEFAULT / AOUT_MAX_INPUT_RATE &&
            i_input_rate <= INPUT_RATE_DEFAULT * AOUT_MAX_INPUT_RATE );
306

307
    assert( p_buffer->i_pts > 0 );
308

309
    p_buffer->i_length = (mtime_t)p_buffer->i_nb_samples * 1000000
310 311
                                / p_input->input.i_rate;

312
    aout_lock_input( p_aout, p_input );
313

314
    if( p_input->b_error )
315
    {
316
        aout_unlock_input( p_aout, p_input );
317 318 319 320
        aout_BufferFree( p_buffer );
        return -1;
    }

321
    if( p_input->b_changed )
322 323 324 325 326 327
    {
        /* Maybe the allocation size has changed. Re-allocate a buffer. */
        aout_buffer_t * p_new_buffer;
        mtime_t duration = (1000000 * (mtime_t)p_buffer->i_nb_samples)
                            / p_input->input.i_rate;

328
        p_new_buffer = aout_BufferAlloc( &p_input->input_alloc, duration, NULL);
329 330
        vlc_memcpy( p_new_buffer->p_buffer, p_buffer->p_buffer,
                    p_buffer->i_nb_bytes );
331 332
        p_new_buffer->i_nb_samples = p_buffer->i_nb_samples;
        p_new_buffer->i_nb_bytes = p_buffer->i_nb_bytes;
333
        p_new_buffer->i_pts = p_buffer->i_pts;
334
        p_new_buffer->i_length = p_buffer->i_length;
335 336
        aout_BufferFree( p_buffer );
        p_buffer = p_new_buffer;
Laurent Aimar's avatar
Laurent Aimar committed
337
        p_input->b_changed = false;
338 339
    }

340
    int i_ret = aout_InputPlay( p_aout, p_input, p_buffer, i_input_rate );
341 342

    aout_unlock_input( p_aout, p_input );
343

344 345
    if( i_ret == -1 )
        return -1;
346 347

    /* Run the mixer if it is able to run. */
348
    aout_lock_mixer( p_aout );
349

350
    aout_MixerRun( p_aout );
351

352
    aout_unlock_mixer( p_aout );
353 354 355

    return 0;
}
356 357 358 359 360 361 362 363 364 365 366

int aout_DecGetResetLost( aout_instance_t *p_aout, aout_input_t *p_input )
{
    aout_lock_input( p_aout, p_input );
    int i_value = p_input->i_buffer_lost;
    p_input->i_buffer_lost = 0;
    aout_unlock_input( p_aout, p_input );

    return i_value;
}

Laurent Aimar's avatar
Laurent Aimar committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
void aout_DecChangePause( aout_instance_t *p_aout, aout_input_t *p_input, bool b_paused, mtime_t i_date )
{
    mtime_t i_duration = 0;
    aout_lock_input( p_aout, p_input );
    assert( !p_input->b_paused || !b_paused );
    if( p_input->b_paused )
    {
        i_duration = i_date - p_input->i_pause_date;
    }
    p_input->b_paused = b_paused;
    p_input->i_pause_date = i_date;
    aout_unlock_input( p_aout, p_input );

    if( i_duration != 0 )
    {
        aout_lock_mixer( p_aout );
383
        for( aout_buffer_t *p = p_input->mixer.fifo.p_first; p != NULL; p = p->p_next )
Laurent Aimar's avatar
Laurent Aimar committed
384
        {
385
            p->i_pts += i_duration;
Laurent Aimar's avatar
Laurent Aimar committed
386 387 388 389 390
        }
        aout_unlock_mixer( p_aout );
    }
}

Laurent Aimar's avatar
Laurent Aimar committed
391 392 393 394
void aout_DecFlush( aout_instance_t *p_aout, aout_input_t *p_input )
{
    aout_lock_input_fifos( p_aout );

395 396
    aout_FifoSet( p_aout, &p_input->mixer.fifo, 0 );
    p_input->mixer.begin = NULL;
Laurent Aimar's avatar
Laurent Aimar committed
397 398 399 400

    aout_unlock_input_fifos( p_aout );
}