decoder.c 71.7 KB
Newer Older
1
/*****************************************************************************
2
 * decoder.c: Functions for the management of decoders
3
 *****************************************************************************
4
 * Copyright (C) 1999-2004 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
8
 *          Gildas Bazin <gbazin@videolan.org>
9
 *          Laurent Aimar <fenrir@via.ecp.fr>
10 11 12 13 14
 *
 * 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.
15
 *
16 17 18 19 20 21 22
 * 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
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
32
#include <assert.h>
33

34
#include <vlc_common.h>
35

Clément Stenac's avatar
Clément Stenac committed
36 37 38 39 40 41
#include <vlc_block.h>
#include <vlc_vout.h>
#include <vlc_aout.h>
#include <vlc_sout.h>
#include <vlc_codec.h>
#include <vlc_osd.h>
Laurent Aimar's avatar
Laurent Aimar committed
42
#include <vlc_meta.h>
43
#include <vlc_dialog.h>
Clément Stenac's avatar
Clément Stenac committed
44 45 46

#include "audio_output/aout_internal.h"
#include "stream_output/stream_output.h"
47
#include "input_internal.h"
48 49
#include "clock.h"
#include "decoder.h"
Christophe Mutricy's avatar
Christophe Mutricy committed
50
#include "event.h"
51
#include "resource.h"
52

53
#include "../video_output/vout_control.h"
Laurent Aimar's avatar
Laurent Aimar committed
54

55 56
static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, bool,
                                 sout_instance_t *p_sout );
57
static void       DeleteDecoder( decoder_t * );
58

59
static void      *DecoderThread( vlc_object_t * );
60 61
static void       DecoderProcess( decoder_t *, block_t * );
static void       DecoderError( decoder_t *p_dec, block_t *p_block );
Laurent Aimar's avatar
Laurent Aimar committed
62 63
static void       DecoderOutputChangePause( decoder_t *, bool b_paused, mtime_t i_date );
static void       DecoderFlush( decoder_t * );
64 65
static void       DecoderSignalBuffering( decoder_t *, bool );
static void       DecoderFlushBuffering( decoder_t * );
Sam Hocevar's avatar
 
Sam Hocevar committed
66

Laurent Aimar's avatar
Laurent Aimar committed
67 68
static void       DecoderUnsupportedCodec( decoder_t *, vlc_fourcc_t );

Gildas Bazin's avatar
 
Gildas Bazin committed
69 70 71 72 73 74
/* Buffers allocation callbacks for the decoders */
static aout_buffer_t *aout_new_buffer( decoder_t *, int );
static void aout_del_buffer( decoder_t *, aout_buffer_t * );

static picture_t *vout_new_buffer( decoder_t * );
static void vout_del_buffer( decoder_t *, picture_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
75 76
static void vout_link_picture( decoder_t *, picture_t * );
static void vout_unlink_picture( decoder_t *, picture_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
77

78 79 80
static subpicture_t *spu_new_buffer( decoder_t * );
static void spu_del_buffer( decoder_t *, subpicture_t * );

81 82
struct decoder_owner_sys_t
{
Laurent Aimar's avatar
Laurent Aimar committed
83 84
    int64_t         i_preroll_end;

85
    input_thread_t  *p_input;
86
    input_clock_t   *p_clock;
87
    int             i_last_rate;
88

89 90
    vout_thread_t   *p_spu_vout;
    int              i_spu_channel;
91
    int64_t          i_spu_order;
92

93 94
    sout_instance_t         *p_sout;
    sout_packetizer_input_t *p_sout_input;
95

96 97
    /* Some decoders require already packetized data (ie. not truncated) */
    decoder_t *p_packetizer;
98
    bool b_packetizer;
99

100 101 102 103 104
    /* Current format in use by the output */
    video_format_t video;
    audio_format_t audio;
    es_format_t    sout;

105 106 107
    /* */
    bool           b_fmt_description;
    es_format_t    fmt_description;
Laurent Aimar's avatar
Laurent Aimar committed
108
    vlc_meta_t     *p_description;
109

110 111
    /* fifo */
    block_fifo_t *p_fifo;
112

113 114
    /* Lock for communication with decoder thread */
    vlc_mutex_t lock;
115 116
    vlc_cond_t  wait_request;
    vlc_cond_t  wait_acknowledge;
117

Laurent Aimar's avatar
Laurent Aimar committed
118 119 120 121 122 123 124
    /* -- These variables need locking on write(only) -- */
    aout_instance_t *p_aout;
    aout_input_t    *p_aout_input;

    vout_thread_t   *p_vout;

    /* -- Theses variables need locking on read *and* write -- */
125
    /* */
Laurent Aimar's avatar
Laurent Aimar committed
126
    /* Pause */
127
    bool b_paused;
128 129 130
    struct
    {
        mtime_t i_date;
131
        int     i_ignore;
132 133 134 135 136 137
    } pause;

    /* Buffering */
    bool b_buffering;
    struct
    {
138
        bool b_first;
139 140 141 142 143 144 145 146 147 148 149
        bool b_full;
        int  i_count;

        picture_t     *p_picture;
        picture_t     **pp_picture_next;

        subpicture_t  *p_subpic;
        subpicture_t  **pp_subpic_next;

        aout_buffer_t *p_audio;
        aout_buffer_t **pp_audio_next;
150 151 152

        block_t       *p_block;
        block_t       **pp_block_next;
153
    } buffer;
154

Laurent Aimar's avatar
Laurent Aimar committed
155 156 157
    /* Flushing */
    bool b_flushing;

158
    /* CC */
159 160 161 162 163 164
    struct
    {
        bool b_supported;
        bool pb_present[4];
        decoder_t *pp_decoder[4];
    } cc;
165 166 167

    /* Delay */
    mtime_t i_ts_delay;
168 169
};

170 171 172 173
#define DECODER_MAX_BUFFERING_COUNT (4)
#define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
#define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)

174 175 176 177 178 179 180 181
/* Pictures which are DECODER_BOGUS_VIDEO_DELAY or more in advance probably have
 * a bogus PTS and won't be displayed */
#define DECODER_BOGUS_VIDEO_DELAY                ((mtime_t)(DEFAULT_PTS_DELAY * 30))

/* */
#define DECODER_SPU_VOUT_WAIT_DURATION ((int)(0.200*CLOCK_FREQ))


Laurent Aimar's avatar
Laurent Aimar committed
182 183 184
/*****************************************************************************
 * Public functions
 *****************************************************************************/
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
picture_t *decoder_NewPicture( decoder_t *p_decoder )
{
    picture_t *p_picture = p_decoder->pf_vout_buffer_new( p_decoder );
    if( !p_picture )
        msg_Warn( p_decoder, "can't get output picture" );
    return p_picture;
}
void decoder_DeletePicture( decoder_t *p_decoder, picture_t *p_picture )
{
    p_decoder->pf_vout_buffer_del( p_decoder, p_picture );
}
void decoder_LinkPicture( decoder_t *p_decoder, picture_t *p_picture )
{
    p_decoder->pf_picture_link( p_decoder, p_picture );
}
void decoder_UnlinkPicture( decoder_t *p_decoder, picture_t *p_picture )
{
    p_decoder->pf_picture_unlink( p_decoder, p_picture );
}

205 206 207 208 209 210 211 212 213 214 215
aout_buffer_t *decoder_NewAudioBuffer( decoder_t *p_decoder, int i_size )
{
    if( !p_decoder->pf_aout_buffer_new )
        return NULL;
    return p_decoder->pf_aout_buffer_new( p_decoder, i_size );
}
void decoder_DeleteAudioBuffer( decoder_t *p_decoder, aout_buffer_t *p_buffer )
{
    p_decoder->pf_aout_buffer_del( p_decoder, p_buffer );
}

216 217 218 219 220 221 222 223 224 225 226
subpicture_t *decoder_NewSubpicture( decoder_t *p_decoder )
{
    subpicture_t *p_subpicture = p_decoder->pf_spu_buffer_new( p_decoder );
    if( !p_subpicture )
        msg_Warn( p_decoder, "can't get output subpicture" );
    return p_subpicture;
}
void decoder_DeleteSubpicture( decoder_t *p_decoder, subpicture_t *p_subpicture )
{
    p_decoder->pf_spu_buffer_del( p_decoder, p_subpicture );
}
227

Laurent Aimar's avatar
Laurent Aimar committed
228 229 230 231 232 233
/* decoder_GetInputAttachments:
 */
int decoder_GetInputAttachments( decoder_t *p_dec,
                                 input_attachment_t ***ppp_attachment,
                                 int *pi_attachment )
{
234 235 236 237
    if( !p_dec->pf_get_attachments )
        return VLC_EGENERIC;

    return p_dec->pf_get_attachments( p_dec, ppp_attachment, pi_attachment );
Laurent Aimar's avatar
Laurent Aimar committed
238
}
239 240 241 242
/* decoder_GetDisplayDate:
 */
mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
{
243
    if( !p_dec->pf_get_display_date )
244
        return VLC_TS_INVALID;
245

246
    return p_dec->pf_get_display_date( p_dec, i_ts );
247 248 249 250 251
}
/* decoder_GetDisplayRate:
 */
int decoder_GetDisplayRate( decoder_t *p_dec )
{
252
    if( !p_dec->pf_get_display_rate )
Laurent Aimar's avatar
Laurent Aimar committed
253
        return INPUT_RATE_DEFAULT;
254 255

    return p_dec->pf_get_display_rate( p_dec );
256
}
257

Clément Stenac's avatar
Clément Stenac committed
258 259 260 261 262 263 264
/**
 * Spawns a new decoder thread
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
 * \return the spawned decoder object
 */
265
decoder_t *input_DecoderNew( input_thread_t *p_input,
266 267
                             es_format_t *fmt, input_clock_t *p_clock,
                             sout_instance_t *p_sout  )
268
{
269
    decoder_t *p_dec = NULL;
270
    const char *psz_type = p_sout ? N_("packetizer") : N_("decoder");
271
    int i_priority;
272

273 274 275
    /* Create the decoder configuration structure */
    p_dec = CreateDecoder( p_input, fmt, p_sout != NULL, p_sout );
    if( p_dec == NULL )
276
    {
277 278 279 280 281
        msg_Err( p_input, "could not create %s", psz_type );
        dialog_Fatal( p_input, _("Streaming / Transcoding failed"),
                      _("VLC could not open the %s module."),
                      vlc_gettext( psz_type ) );
        return NULL;
282 283
    }

284
    if( !p_dec->p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
285
    {
286
        DecoderUnsupportedCodec( p_dec, fmt->i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
287 288

        DeleteDecoder( p_dec );
289
        return NULL;
Henri Fallon's avatar
 
Henri Fallon committed
290 291
    }

292
    p_dec->p_owner->p_clock = p_clock;
293
    assert( p_dec->fmt_out.i_cat != UNKNOWN_ES );
294

295
    if( p_dec->fmt_out.i_cat == AUDIO_ES )
296
        i_priority = VLC_THREAD_PRIORITY_AUDIO;
297
    else
298
        i_priority = VLC_THREAD_PRIORITY_VIDEO;
299

300
    /* Spawn the decoder thread */
301
    if( vlc_thread_create( p_dec, "decoder", DecoderThread, i_priority ) )
Henri Fallon's avatar
 
Henri Fallon committed
302
    {
303 304 305 306
        msg_Err( p_dec, "cannot spawn decoder thread" );
        module_unneed( p_dec, p_dec->p_module );
        DeleteDecoder( p_dec );
        return NULL;
Henri Fallon's avatar
 
Henri Fallon committed
307 308
    }

309
    return p_dec;
310 311
}

Clément Stenac's avatar
Clément Stenac committed
312 313 314 315 316 317 318
/**
 * Kills a decoder thread and waits until it's finished
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
 * \return nothing
 */
319
void input_DecoderDelete( decoder_t *p_dec )
320
{
321 322
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

323
    vlc_object_kill( p_dec );
324

325
    /* Make sure we aren't paused/buffering/waiting anymore */
326
    vlc_mutex_lock( &p_owner->lock );
327
    const bool b_was_paused = p_owner->b_paused;
328 329 330
    p_owner->b_paused = false;
    p_owner->b_buffering = false;
    p_owner->b_flushing = true;
331
    vlc_cond_signal( &p_owner->wait_request );
332
    vlc_mutex_unlock( &p_owner->lock );
333

334
    vlc_thread_join( p_dec );
335 336
    p_owner->b_paused = b_was_paused;

337
    module_unneed( p_dec, p_dec->p_module );
338

339
    /* */
340
    if( p_dec->p_owner->cc.b_supported )
341 342 343
    {
        int i;
        for( i = 0; i < 4; i++ )
344
            input_DecoderSetCcState( p_dec, false, i );
345 346
    }

347
    /* Delete decoder */
Gildas Bazin's avatar
 
Gildas Bazin committed
348
    DeleteDecoder( p_dec );
349
}
Clément Stenac's avatar
Clément Stenac committed
350 351

/**
352
 * Put a block_t in the decoder's fifo.
353
 * Thread-safe w.r.t. the decoder. May be a cancellation point.
Clément Stenac's avatar
Clément Stenac committed
354 355 356 357
 *
 * \param p_dec the decoder object
 * \param p_block the data block
 */
358
void input_DecoderDecode( decoder_t *p_dec, block_t *p_block, bool b_do_pace )
359
{
360 361
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

362
    if( b_do_pace )
363
    {
364 365 366 367 368 369
        /* The fifo is not consummed when buffering and so will
         * deadlock vlc.
         * There is no need to lock as b_buffering is never modify
         * inside decoder thread. */
        if( !p_owner->b_buffering )
            block_FifoPace( p_owner->p_fifo, 10, SIZE_MAX );
370
    }
371
#ifdef __arm__
372
    else if( block_FifoSize( p_owner->p_fifo ) > 50*1024*1024 /* 50 MiB */ )
373
#else
374
    else if( block_FifoSize( p_owner->p_fifo ) > 400*1024*1024 /* 400 MiB, ie ~ 50mb/s for 60s */ )
375
#endif
376
    {
377
        /* FIXME: ideally we would check the time amount of data
378
         * in the FIFO instead of its size. */
379 380 381
        msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
                  "consumed quickly enough), resetting fifo!" );
        block_FifoEmpty( p_owner->p_fifo );
382
    }
383 384

    block_FifoPut( p_owner->p_fifo, p_block );
385
}
386

387
bool input_DecoderIsEmpty( decoder_t * p_dec )
Laurent Aimar's avatar
 
Laurent Aimar committed
388
{
Laurent Aimar's avatar
Laurent Aimar committed
389 390
    assert( !p_dec->p_owner->b_buffering );

391 392
    /* FIXME that's not really true */
    return block_FifoCount( p_dec->p_owner->p_fifo ) <= 0;
Laurent Aimar's avatar
 
Laurent Aimar committed
393 394
}

395
void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
396
{
397
    decoder_owner_sys_t *p_owner = p_dec->p_owner;
398 399
    int i;

400
    vlc_mutex_lock( &p_owner->lock );
401
    for( i = 0; i < 4; i++ )
402
        pb_present[i] =  p_owner->cc.pb_present[i];
403
    vlc_mutex_unlock( &p_owner->lock );
404
}
405
int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
406 407 408 409 410
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );

411
    if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
412 413 414 415 416 417 418 419 420 421 422 423 424 425
        return VLC_EGENERIC;

    if( b_decode )
    {
        static const vlc_fourcc_t fcc[4] = {
            VLC_FOURCC('c', 'c', '1', ' '),
            VLC_FOURCC('c', 'c', '2', ' '),
            VLC_FOURCC('c', 'c', '3', ' '),
            VLC_FOURCC('c', 'c', '4', ' '),
        };
        decoder_t *p_cc;
        es_format_t fmt;

        es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
426
        p_cc = CreateDecoder( p_owner->p_input, &fmt, false, p_owner->p_sout );
427 428 429
        if( !p_cc )
        {
            msg_Err( p_dec, "could not create decoder" );
430
            dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
431
                          _("VLC could not open the decoder module.") );
432 433 434 435 436 437 438 439
            return VLC_EGENERIC;
        }
        else if( !p_cc->p_module )
        {
            DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
            DeleteDecoder( p_cc );
            return VLC_EGENERIC;
        }
440
        p_cc->p_owner->p_clock = p_owner->p_clock;
441

442
        vlc_mutex_lock( &p_owner->lock );
443
        p_owner->cc.pp_decoder[i_channel] = p_cc;
444
        vlc_mutex_unlock( &p_owner->lock );
445 446 447 448 449
    }
    else
    {
        decoder_t *p_cc;

450
        vlc_mutex_lock( &p_owner->lock );
451 452
        p_cc = p_owner->cc.pp_decoder[i_channel];
        p_owner->cc.pp_decoder[i_channel] = NULL;
453
        vlc_mutex_unlock( &p_owner->lock );
454 455 456 457

        if( p_cc )
        {
            vlc_object_kill( p_cc );
458
            module_unneed( p_cc, p_cc->p_module );
459 460 461 462 463
            DeleteDecoder( p_cc );
        }
    }
    return VLC_SUCCESS;
}
464
int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
465 466 467
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

468
    *pb_decode = false;
469
    if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
470 471
        return VLC_EGENERIC;

472
    vlc_mutex_lock( &p_owner->lock );
473
    *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
474
    vlc_mutex_unlock( &p_owner->lock );
475 476 477
    return VLC_EGENERIC;
}

478 479 480 481 482 483
void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );

484
    assert( !p_owner->b_paused || !b_paused );
485

486
    p_owner->b_paused = b_paused;
487
    p_owner->pause.i_date = i_date;
488
    p_owner->pause.i_ignore = 0;
489
    vlc_cond_signal( &p_owner->wait_request );
490

Laurent Aimar's avatar
Laurent Aimar committed
491
    DecoderOutputChangePause( p_dec, b_paused, i_date );
492

493 494
    vlc_mutex_unlock( &p_owner->lock );
}
495 496 497 498 499 500 501 502 503

void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );
    p_owner->i_ts_delay = i_delay;
    vlc_mutex_unlock( &p_owner->lock );
}
504

Laurent Aimar's avatar
Laurent Aimar committed
505
void input_DecoderStartBuffering( decoder_t *p_dec )
506 507 508
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

Laurent Aimar's avatar
Laurent Aimar committed
509
    vlc_mutex_lock( &p_owner->lock );
510

Laurent Aimar's avatar
Laurent Aimar committed
511
    DecoderFlush( p_dec );
Laurent Aimar's avatar
Laurent Aimar committed
512

513
    p_owner->buffer.b_first = true;
514 515 516
    p_owner->buffer.b_full = false;
    p_owner->buffer.i_count = 0;

517 518
    assert( !p_owner->buffer.p_picture && !p_owner->buffer.p_subpic &&
            !p_owner->buffer.p_audio && !p_owner->buffer.p_block );
519 520 521 522 523 524 525 526 527 528

    p_owner->buffer.p_picture = NULL;
    p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;

    p_owner->buffer.p_subpic = NULL;
    p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;

    p_owner->buffer.p_audio = NULL;
    p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;

529 530 531 532
    p_owner->buffer.p_block = NULL;
    p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;


533 534
    p_owner->b_buffering = true;

535
    vlc_cond_signal( &p_owner->wait_request );
536

Laurent Aimar's avatar
Laurent Aimar committed
537 538
    vlc_mutex_unlock( &p_owner->lock );
}
Laurent Aimar's avatar
Laurent Aimar committed
539

Laurent Aimar's avatar
Laurent Aimar committed
540 541
void input_DecoderStopBuffering( decoder_t *p_dec )
{
542 543 544 545 546 547
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );

    p_owner->b_buffering = false;

548
    vlc_cond_signal( &p_owner->wait_request );
549 550

    vlc_mutex_unlock( &p_owner->lock );
551 552
}

553 554 555 556 557 558 559 560 561
void input_DecoderWaitBuffering( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );

    while( vlc_object_alive( p_dec ) && p_owner->b_buffering && !p_owner->buffer.b_full )
    {
        block_FifoWake( p_owner->p_fifo );
562
        vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
563 564 565 566
    }

    vlc_mutex_unlock( &p_owner->lock );
}
567

568 569 570 571 572 573 574
void input_DecoderFrameNext( decoder_t *p_dec, mtime_t *pi_duration )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    *pi_duration = 0;

    vlc_mutex_lock( &p_owner->lock );
575
    if( p_dec->fmt_out.i_cat == VIDEO_ES )
576
    {
577 578 579 580
        if( p_owner->b_paused && p_owner->p_vout )
        {
            vout_NextPicture( p_owner->p_vout, pi_duration );
            p_owner->pause.i_ignore++;
581
            vlc_cond_signal( &p_owner->wait_request );
582 583 584 585 586 587
        }
    }
    else
    {
        /* TODO subtitle should not be flushed */
        DecoderFlush( p_dec );
588 589 590
    }
    vlc_mutex_unlock( &p_owner->lock );
}
591

Laurent Aimar's avatar
Laurent Aimar committed
592
bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
593 594 595 596 597 598 599 600 601 602
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;
    bool b_changed;

    vlc_mutex_lock( &p_owner->lock );
    b_changed = p_owner->b_fmt_description;
    if( b_changed )
    {
        if( p_fmt )
            es_format_Copy( p_fmt, &p_owner->fmt_description );
Laurent Aimar's avatar
Laurent Aimar committed
603 604 605 606 607 608 609 610 611 612 613

        if( pp_meta )
        {
            *pp_meta = NULL;
            if( p_owner->p_description )
            {
                *pp_meta = vlc_meta_New();
                if( *pp_meta )
                    vlc_meta_Merge( *pp_meta, p_owner->p_description );
            }
        }
614 615 616 617 618 619
        p_owner->b_fmt_description = false;
    }
    vlc_mutex_unlock( &p_owner->lock );
    return b_changed;
}

620 621 622 623 624 625 626
size_t input_DecoderGetFifoSize( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    return block_FifoSize( p_owner->p_fifo );
}

627 628 629 630 631 632 633
void input_DecoderGetObjects( decoder_t *p_dec,
                              vout_thread_t **pp_vout, aout_instance_t **pp_aout )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );
    if( pp_vout )
Laurent Aimar's avatar
Laurent Aimar committed
634
        *pp_vout = p_owner->p_vout ? vlc_object_hold( p_owner->p_vout ) : NULL;
635
    if( pp_aout )
Laurent Aimar's avatar
Laurent Aimar committed
636
        *pp_aout = p_owner->p_aout ? vlc_object_hold( p_owner->p_aout ) : NULL;
637 638 639
    vlc_mutex_unlock( &p_owner->lock );
}

Laurent Aimar's avatar
Laurent Aimar committed
640 641 642
/*****************************************************************************
 * Internal functions
 *****************************************************************************/
643 644 645 646 647 648 649 650 651 652 653 654 655
static int DecoderGetInputAttachments( decoder_t *p_dec,
                                       input_attachment_t ***ppp_attachment,
                                       int *pi_attachment )
{
    return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
                          ppp_attachment, pi_attachment );
}
static mtime_t DecoderGetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );
    if( p_owner->b_buffering || p_owner->b_paused )
656
        i_ts = VLC_TS_INVALID;
657 658
    vlc_mutex_unlock( &p_owner->lock );

659
    if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
660 661
        return i_ts;

662
    if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
663
        return VLC_TS_INVALID;
664 665

    return i_ts;
666 667 668 669 670 671 672 673 674
}
static int DecoderGetDisplayRate( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    if( !p_owner->p_clock )
        return INPUT_RATE_DEFAULT;
    return input_clock_GetRate( p_owner->p_clock );
}
Laurent Aimar's avatar
Laurent Aimar committed
675 676 677 678

/* */
static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
{
679
    msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
Laurent Aimar's avatar
Laurent Aimar committed
680 681
             "VLC probably does not support this sound or video format.",
             (char*)&codec );
682 683 684 685
    dialog_Fatal( p_dec, _("No suitable decoder module"),
                 _("VLC does not support the audio or video format \"%4.4s\". "
                  "Unfortunately there is no way for you to fix this."),
                  (char*)&codec );
Laurent Aimar's avatar
Laurent Aimar committed
686 687 688
}


Clément Stenac's avatar
Clément Stenac committed
689 690 691 692 693
/**
 * Create a decoder object
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
694
 * \param b_packetizer instead of a decoder
Clément Stenac's avatar
Clément Stenac committed
695 696
 * \return the decoder object
 */
697
static decoder_t * CreateDecoder( input_thread_t *p_input,
698 699
                                  es_format_t *fmt, bool b_packetizer,
                                  sout_instance_t *p_sout )
Sam Hocevar's avatar
 
Sam Hocevar committed
700
{
Gildas Bazin's avatar
 
Gildas Bazin committed
701
    decoder_t *p_dec;
702
    decoder_owner_sys_t *p_owner;
703 704
    es_format_t null_es_format;

705 706
    p_dec = vlc_custom_create( p_input, sizeof( *p_dec ), VLC_OBJECT_DECODER,
                               "decoder" );
Gildas Bazin's avatar
 
Gildas Bazin committed
707
    if( p_dec == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
708 709
        return NULL;

710 711 712 713 714
    p_dec->pf_decode_audio = NULL;
    p_dec->pf_decode_video = NULL;
    p_dec->pf_decode_sub = NULL;
    p_dec->pf_get_cc = NULL;
    p_dec->pf_packetize = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
715

716
    /* Initialize the decoder */
Gildas Bazin's avatar
 
Gildas Bazin committed
717 718
    p_dec->p_module = NULL;

719
    memset( &null_es_format, 0, sizeof(es_format_t) );
720 721
    es_format_Copy( &p_dec->fmt_in, fmt );
    es_format_Copy( &p_dec->fmt_out, &null_es_format );
Gildas Bazin's avatar
 
Gildas Bazin committed
722

723 724
    p_dec->p_description = NULL;

Gildas Bazin's avatar
 
Gildas Bazin committed
725
    /* Allocate our private structure for the decoder */
726
    p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
727
    if( p_dec->p_owner == NULL )
728 729
    {
        vlc_object_release( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
730
        return NULL;
731
    }
732
    p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
733
    p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
734
    p_dec->p_owner->p_input = p_input;
Gildas Bazin's avatar
 
Gildas Bazin committed
735 736 737
    p_dec->p_owner->p_aout = NULL;
    p_dec->p_owner->p_aout_input = NULL;
    p_dec->p_owner->p_vout = NULL;
738 739
    p_dec->p_owner->p_spu_vout = NULL;
    p_dec->p_owner->i_spu_channel = 0;
740
    p_dec->p_owner->i_spu_order = 0;
741
    p_dec->p_owner->p_sout = p_sout;
742
    p_dec->p_owner->p_sout_input = NULL;
743
    p_dec->p_owner->p_packetizer = NULL;
744
    p_dec->p_owner->b_packetizer = b_packetizer;
745

746
    /* decoder fifo */
747
    if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
748 749 750
    {
        free( p_dec->p_owner );
        vlc_object_release( p_dec );
751
        return NULL;
752
    }
753

Gildas Bazin's avatar
 
Gildas Bazin committed
754 755 756 757 758
    /* Set buffers allocation callbacks for the decoders */
    p_dec->pf_aout_buffer_new = aout_new_buffer;
    p_dec->pf_aout_buffer_del = aout_del_buffer;
    p_dec->pf_vout_buffer_new = vout_new_buffer;
    p_dec->pf_vout_buffer_del = vout_del_buffer;
Gildas Bazin's avatar
 
Gildas Bazin committed
759 760
    p_dec->pf_picture_link    = vout_link_picture;
    p_dec->pf_picture_unlink  = vout_unlink_picture;
761 762
    p_dec->pf_spu_buffer_new  = spu_new_buffer;
    p_dec->pf_spu_buffer_del  = spu_del_buffer;
763 764 765 766
    /* */
    p_dec->pf_get_attachments  = DecoderGetInputAttachments;
    p_dec->pf_get_display_date = DecoderGetDisplayDate;
    p_dec->pf_get_display_rate = DecoderGetDisplayRate;
Gildas Bazin's avatar
 
Gildas Bazin committed
767

Gildas Bazin's avatar
 
Gildas Bazin committed
768 769
    vlc_object_attach( p_dec, p_input );

770
    /* Find a suitable decoder/packetizer module */
771
    if( !b_packetizer )
772
        p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
773
    else
774
        p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
775 776

    /* Check if decoder requires already packetized data */
777
    if( !b_packetizer &&
778 779 780
        p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
    {
        p_dec->p_owner->p_packetizer =
781 782
            vlc_custom_create( p_input, sizeof( decoder_t ),
                               VLC_OBJECT_DECODER, "packetizer" );
783 784 785 786 787
        if( p_dec->p_owner->p_packetizer )
        {
            es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
                            &p_dec->fmt_in );

788 789 790
            es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
                            &null_es_format );

791 792 793
            vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );

            p_dec->p_owner->p_packetizer->p_module =
794
                module_need( p_dec->p_owner->p_packetizer,
795
                             "packetizer", "$packetizer", false );
796 797 798 799

            if( !p_dec->p_owner->p_packetizer->p_module )
            {
                es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
800
                vlc_object_release( p_dec->p_owner->p_packetizer );
801 802 803 804
            }
        }
    }

805 806 807
    /* Copy ourself the input replay gain */
    if( fmt->i_cat == AUDIO_ES )
    {
808
        for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
809 810 811 812 813 814 815 816 817 818 819 820 821
        {
            if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
            {
                p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
                p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
            }
            if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
            {
                p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
                p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
            }
        }
    }
822 823 824

    /* */
    vlc_mutex_init( &p_owner->lock );
825 826
    vlc_cond_init( &p_owner->wait_request );
    vlc_cond_init( &p_owner->wait_acknowledge );
827

828 829
    p_owner->b_fmt_description = false;
    es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
Laurent Aimar's avatar
Laurent Aimar committed
830
    p_owner->p_description = NULL;
831

832
    p_owner->b_paused = false;
833
    p_owner->pause.i_date = VLC_TS_INVALID;
834
    p_owner->pause.i_ignore = 0;
835 836

    p_owner->b_buffering = false;
837
    p_owner->buffer.b_first = true;
838 839 840 841 842
    p_owner->buffer.b_full = false;
    p_owner->buffer.i_count = 0;
    p_owner->buffer.p_picture = NULL;
    p_owner->buffer.p_subpic = NULL;
    p_owner->buffer.p_audio = NULL;
843
    p_owner->buffer.p_block = NULL;
844 845 846

    p_owner->b_flushing = false;

847
    /* */
848
    p_owner->cc.b_supported = false;
849
    if( !b_packetizer )
850 851
    {
        if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
852
            p_owner->cc.b_supported = true;
853
        if( p_dec->pf_get_cc )
854
            p_owner->cc.b_supported = true;
855 856
    }

857
    for( unsigned i = 0; i < 4; i++ )
858
    {
859 860
        p_owner->cc.pb_present[i] = false;
        p_owner->cc.pp_decoder[i] = NULL;
861
    }
862
    p_owner->i_ts_delay = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
863 864 865
    return p_dec;
}

Clément Stenac's avatar
Clément Stenac committed
866 867 868 869 870
/**
 * The decoding main loop
 *
 * \param p_dec the decoder
 */
871
static void *DecoderThread( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
872
{
873 874 875
    decoder_t *p_dec = (decoder_t *)p_this;
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

Gildas Bazin's avatar
 
Gildas Bazin committed
876
    /* The decoder's main loop */
877
    for( ;; )
878
    {
879
        block_t *p_block = block_FifoGet( p_owner->p_fifo );
880

881 882
        /* Make sure there is no cancellation point other than this one^^.
         * If you need one, be sure to push cleanup of p_block. */
883 884
        DecoderSignalBuffering( p_dec, p_block == NULL );

885
        if( p_block )
886
        {
887 888
            int canc = vlc_savecancel();

889
            if( p_dec->b_error )
890
                DecoderError( p_dec, p_block );
891 892
            else
                DecoderProcess( p_dec, p_block );
893 894

            vlc_restorecancel( canc );
895
        }
896
    }
897
    return NULL;
898
}
Gildas Bazin's avatar
 
Gildas Bazin committed
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913
static block_t *DecoderBlockFlushNew()
{
    block_t *p_null = block_Alloc( 128 );
    if( !p_null )
        return NULL;

    p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY |
                       BLOCK_FLAG_CORRUPTED |
                       BLOCK_FLAG_CORE_FLUSH;
    memset( p_null->p_buffer, 0, p_null->i_buffer );

    return p_null;
}

Laurent Aimar's avatar
Laurent Aimar committed
914 915 916 917
static void DecoderFlush( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

918
    vlc_assert_locked( &p_owner->lock );
Laurent Aimar's avatar
Laurent Aimar committed
919

920 921
    /* Empty the fifo */
    block_FifoEmpty( p_owner->p_fifo );
Laurent Aimar's avatar
Laurent Aimar committed
922

923 924
    /* Monitor for flush end */
    p_owner->b_flushing = true;
925
    vlc_cond_signal( &p_owner->wait_request );
Laurent Aimar's avatar
Laurent Aimar committed
926 927

    /* Send a special block */
928
    block_t *p_null = DecoderBlockFlushNew();
Laurent Aimar's avatar
Laurent Aimar committed
929 930
    if( !p_null )
        return;
931
    input_DecoderDecode( p_dec, p_null, false );
Laurent Aimar's avatar
Laurent Aimar committed
932 933

    /* */
934
    while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
935
        vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
Laurent Aimar's avatar
Laurent Aimar committed
936 937
}

938 939 940 941 942 943 944 945 946 947
static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_mutex_lock( &p_owner->lock );

    if( p_owner->b_buffering )
    {
        if( b_full )
            p_owner->buffer.b_full = true;
948
        vlc_cond_signal( &p_owner->wait_acknowledge );
949 950 951 952 953
    }

    vlc_mutex_unlock( &p_owner->lock );
}

Laurent Aimar's avatar
Laurent Aimar committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967
static bool DecoderIsFlushing( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;
    bool b_flushing;

    vlc_mutex_lock( &p_owner->lock );

    b_flushing = p_owner->b_flushing;

    vlc_mutex_unlock( &p_owner->lock );

    return b_flushing;
}

968 969 970 971 972 973 974 975
static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

    vlc_assert_locked( &p_owner->lock );

    while( !p_owner->b_flushing )
    {
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
        if( p_owner->b_paused )
        {
            if( p_owner->b_buffering && !p_owner->buffer.b_full )
                break;
            if( p_owner->pause.i_ignore > 0 )
            {
                p_owner->pause.i_ignore--;
                break;
            }
        }
        else
        {
            if( !p_owner->b_buffering || !p_owner->buffer.b_full )
                break;
        }
991
        vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
992 993 994 995 996 997
    }

    if( pb_reject )
        *pb_reject = p_owner->b_flushing;
}

Laurent Aimar's avatar
Laurent Aimar committed
998 999 1000 1001
static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

Laurent Aimar's avatar
Laurent Aimar committed
1002 1003
    vlc_assert_locked( &p_owner->lock );

Laurent Aimar's avatar
Laurent Aimar committed
1004 1005 1006 1007
    /* XXX only audio and video output have to be paused.
     * - for sout it is useless
     * - for subs, it is done by the vout
     */
1008
    if( p_dec->fmt_out.i_cat == AUDIO_ES )
Laurent Aimar's avatar
Laurent Aimar committed
1009
    {
Laurent Aimar's avatar
Laurent Aimar committed
1010 1011 1012
        if( p_owner->p_aout && p_owner->p_aout_input )
            aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
                                 b_paused, i_date );
Laurent Aimar's avatar
Laurent Aimar committed
1013
    }
1014
    else if( p_dec->fmt_out.i_cat == VIDEO_ES )
Laurent Aimar's avatar
Laurent Aimar committed
1015 1016 1017 1018 1019
    {
        if( p_owner->p_vout )
            vout_ChangePause( p_owner->p_vout, b_paused, i_date );
    }
}
Laurent Aimar's avatar
Laurent Aimar committed
1020 1021
static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
{
1022 1023
    if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
        *pi_preroll = INT64_MAX;
1024
    else if( p->i_dts > VLC_TS_INVALID )
Laurent Aimar's avatar
Laurent Aimar committed
1025
        *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1026
    else if( p->i_pts > VLC_TS_INVALID )
Laurent Aimar's avatar
Laurent Aimar committed
1027
        *pi_preroll = __MIN( *pi_preroll, p->i_pts );
Laurent Aimar's avatar
Laurent Aimar committed
1028
}
1029

1030
static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1031 1032 1033 1034
{
    mtime_t current_date = mdate();

    /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1035
    if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1036 1037 1038
    {
        /* ETSI EN 300 472 Annex A : do not take into account the PTS
         * for teletext streams. */
1039
        return current_date + 400000;
1040 1041 1042 1043
    }
    return i_ts;
}

Laurent Aimar's avatar
Laurent Aimar committed
1044
static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1045
                          mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1046
{
Laurent Aimar's avatar
Laurent Aimar committed
1047 1048
    decoder_owner_sys_t *p_owner = p_dec->p_owner;
    input_clock_t   *p_clock = p_owner->p_clock;
1049

Laurent Aimar's avatar
Laurent Aimar committed
1050
    vlc_assert_locked( &p_owner->lock );
Laurent Aimar's avatar