decoder.c 71.2 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
        vlc_object_release( p_dec );
290
        return NULL;
Henri Fallon's avatar
 
Henri Fallon committed
291 292
    }

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

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

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

311
    return p_dec;
312 313
}

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

325
    vlc_object_kill( p_dec );
326

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

336
    vlc_thread_join( p_dec );
337 338
    p_owner->b_paused = b_was_paused;

339
    module_unneed( p_dec, p_dec->p_module );
340

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

Gildas Bazin's avatar
 
Gildas Bazin committed
349 350 351 352
    /* Delete decoder configuration */
    DeleteDecoder( p_dec );

    /* Delete the decoder */
353
    vlc_object_release( p_dec );
354
}
Clément Stenac's avatar
Clément Stenac committed
355 356

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

367
    if( b_do_pace )
368
    {
369 370 371 372 373 374
        /* 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 );
375
    }
376
    else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
377
    {
378
        /* FIXME: ideally we would check the time amount of data
379
         * in the FIFO instead of its size. */
380 381 382
        msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
                  "consumed quickly enough), resetting fifo!" );
        block_FifoEmpty( p_owner->p_fifo );
383
    }
384 385

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

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

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

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

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

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

412
    if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
413 414 415 416 417 418 419 420 421 422 423 424 425 426
        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] );
427
        p_cc = CreateDecoder( p_owner->p_input, &fmt, false, p_owner->p_sout );
428 429 430
        if( !p_cc )
        {
            msg_Err( p_dec, "could not create decoder" );
431
            dialog_Fatal( p_dec, _("Streaming / Transcoding failed"), "%s",
432
                          _("VLC could not open the decoder module.") );
433 434 435 436 437 438
            return VLC_EGENERIC;
        }
        else if( !p_cc->p_module )
        {
            DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
            DeleteDecoder( p_cc );
439
            vlc_object_release( p_cc );
440 441
            return VLC_EGENERIC;
        }
442
        p_cc->p_owner->p_clock = p_owner->p_clock;
443

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

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

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

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

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

481 482 483 484 485 486
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 );

487
    assert( !p_owner->b_paused || !b_paused );
488

489
    p_owner->b_paused = b_paused;
490
    p_owner->pause.i_date = i_date;
491
    p_owner->pause.i_ignore = 0;
492
    vlc_cond_signal( &p_owner->wait_request );
493

Laurent Aimar's avatar
Laurent Aimar committed
494
    DecoderOutputChangePause( p_dec, b_paused, i_date );
495

496 497
    vlc_mutex_unlock( &p_owner->lock );
}
498 499 500 501 502 503 504 505 506

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 );
}
507

Laurent Aimar's avatar
Laurent Aimar committed
508
void input_DecoderStartBuffering( decoder_t *p_dec )
509 510 511
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

Laurent Aimar's avatar
Laurent Aimar committed
512
    vlc_mutex_lock( &p_owner->lock );
513

Laurent Aimar's avatar
Laurent Aimar committed
514
    DecoderFlush( p_dec );
Laurent Aimar's avatar
Laurent Aimar committed
515

516
    p_owner->buffer.b_first = true;
517 518 519
    p_owner->buffer.b_full = false;
    p_owner->buffer.i_count = 0;

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

    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;

532 533 534 535
    p_owner->buffer.p_block = NULL;
    p_owner->buffer.pp_block_next = &p_owner->buffer.p_block;


536 537
    p_owner->b_buffering = true;

538
    vlc_cond_signal( &p_owner->wait_request );
539

Laurent Aimar's avatar
Laurent Aimar committed
540 541
    vlc_mutex_unlock( &p_owner->lock );
}
Laurent Aimar's avatar
Laurent Aimar committed
542

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

    vlc_mutex_lock( &p_owner->lock );

    p_owner->b_buffering = false;

551
    vlc_cond_signal( &p_owner->wait_request );
552 553

    vlc_mutex_unlock( &p_owner->lock );
554 555
}

556 557 558 559 560 561 562 563 564
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 );
565
        vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
566 567 568 569
    }

    vlc_mutex_unlock( &p_owner->lock );
}
570

571 572 573 574 575 576 577
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 );
578
    if( p_dec->fmt_out.i_cat == VIDEO_ES )
579
    {
580 581 582 583
        if( p_owner->b_paused && p_owner->p_vout )
        {
            vout_NextPicture( p_owner->p_vout, pi_duration );
            p_owner->pause.i_ignore++;
584
            vlc_cond_signal( &p_owner->wait_request );
585 586 587 588 589 590
        }
    }
    else
    {
        /* TODO subtitle should not be flushed */
        DecoderFlush( p_dec );
591 592 593
    }
    vlc_mutex_unlock( &p_owner->lock );
}
594

Laurent Aimar's avatar
Laurent Aimar committed
595
bool input_DecoderHasFormatChanged( decoder_t *p_dec, es_format_t *p_fmt, vlc_meta_t **pp_meta )
596 597 598 599 600 601 602 603 604 605
{
    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
606 607 608 609 610 611 612 613 614 615 616

        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 );
            }
        }
617 618 619 620 621 622
        p_owner->b_fmt_description = false;
    }
    vlc_mutex_unlock( &p_owner->lock );
    return b_changed;
}

Laurent Aimar's avatar
Laurent Aimar committed
623 624 625
/*****************************************************************************
 * Internal functions
 *****************************************************************************/
626 627 628 629 630 631 632 633 634 635 636 637 638
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 )
639
        i_ts = VLC_TS_INVALID;
640 641
    vlc_mutex_unlock( &p_owner->lock );

642
    if( !p_owner->p_clock || i_ts <= VLC_TS_INVALID )
643 644
        return i_ts;

645
    if( input_clock_ConvertTS( p_owner->p_clock, NULL, &i_ts, NULL, INT64_MAX ) )
646
        return VLC_TS_INVALID;
647 648

    return i_ts;
649 650 651 652 653 654 655 656 657
}
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
658 659 660 661

/* */
static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
{
662
    msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'. "
Laurent Aimar's avatar
Laurent Aimar committed
663 664
             "VLC probably does not support this sound or video format.",
             (char*)&codec );
665 666 667 668
    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
669 670 671
}


Clément Stenac's avatar
Clément Stenac committed
672 673 674 675 676
/**
 * Create a decoder object
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
677
 * \param b_packetizer instead of a decoder
Clément Stenac's avatar
Clément Stenac committed
678 679
 * \return the decoder object
 */
680
static decoder_t * CreateDecoder( input_thread_t *p_input,
681 682
                                  es_format_t *fmt, bool b_packetizer,
                                  sout_instance_t *p_sout )
Sam Hocevar's avatar
 
Sam Hocevar committed
683
{
Gildas Bazin's avatar
 
Gildas Bazin committed
684
    decoder_t *p_dec;
685
    decoder_owner_sys_t *p_owner;
686 687
    es_format_t null_es_format;

688
    p_dec = vlc_object_create( p_input, VLC_OBJECT_DECODER );
Gildas Bazin's avatar
 
Gildas Bazin committed
689
    if( p_dec == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
690 691
        return NULL;

692 693 694 695 696
    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
697

698
    /* Initialize the decoder */
Gildas Bazin's avatar
 
Gildas Bazin committed
699 700
    p_dec->p_module = NULL;

701
    memset( &null_es_format, 0, sizeof(es_format_t) );
702 703
    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
704

705 706
    p_dec->p_description = NULL;

Gildas Bazin's avatar
 
Gildas Bazin committed
707
    /* Allocate our private structure for the decoder */
708
    p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
709
    if( p_dec->p_owner == NULL )
710 711
    {
        vlc_object_release( p_dec );
Gildas Bazin's avatar
 
Gildas Bazin committed
712
        return NULL;
713
    }
714
    p_dec->p_owner->i_preroll_end = VLC_TS_INVALID;
715
    p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
716
    p_dec->p_owner->p_input = p_input;
Gildas Bazin's avatar
 
Gildas Bazin committed
717 718 719
    p_dec->p_owner->p_aout = NULL;
    p_dec->p_owner->p_aout_input = NULL;
    p_dec->p_owner->p_vout = NULL;
720 721
    p_dec->p_owner->p_spu_vout = NULL;
    p_dec->p_owner->i_spu_channel = 0;
722
    p_dec->p_owner->i_spu_order = 0;
723
    p_dec->p_owner->p_sout = p_sout;
724
    p_dec->p_owner->p_sout_input = NULL;
725
    p_dec->p_owner->p_packetizer = NULL;
726
    p_dec->p_owner->b_packetizer = b_packetizer;
727

728
    /* decoder fifo */
729
    if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
730 731 732
    {
        free( p_dec->p_owner );
        vlc_object_release( p_dec );
733
        return NULL;
734
    }
735

Gildas Bazin's avatar
 
Gildas Bazin committed
736 737 738 739 740
    /* 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
741 742
    p_dec->pf_picture_link    = vout_link_picture;
    p_dec->pf_picture_unlink  = vout_unlink_picture;
743 744
    p_dec->pf_spu_buffer_new  = spu_new_buffer;
    p_dec->pf_spu_buffer_del  = spu_del_buffer;
745 746 747 748
    /* */
    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
749

Gildas Bazin's avatar
 
Gildas Bazin committed
750 751
    vlc_object_attach( p_dec, p_input );

752
    /* Find a suitable decoder/packetizer module */
753
    if( !b_packetizer )
754
        p_dec->p_module = module_need( p_dec, "decoder", "$codec", false );
755
    else
756
        p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", false );
757 758

    /* Check if decoder requires already packetized data */
759
    if( !b_packetizer &&
760 761 762
        p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
    {
        p_dec->p_owner->p_packetizer =
763
            vlc_object_create( p_input, VLC_OBJECT_DECODER );
764 765 766 767 768
        if( p_dec->p_owner->p_packetizer )
        {
            es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
                            &p_dec->fmt_in );

769 770 771
            es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
                            &null_es_format );

772 773 774
            vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );

            p_dec->p_owner->p_packetizer->p_module =
775
                module_need( p_dec->p_owner->p_packetizer,
776
                             "packetizer", "$packetizer", false );
777 778 779 780 781

            if( !p_dec->p_owner->p_packetizer->p_module )
            {
                es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
                vlc_object_detach( p_dec->p_owner->p_packetizer );
782
                vlc_object_release( p_dec->p_owner->p_packetizer );
783 784 785 786
            }
        }
    }

787 788 789
    /* Copy ourself the input replay gain */
    if( fmt->i_cat == AUDIO_ES )
    {
790
        for( unsigned i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
791 792 793 794 795 796 797 798 799 800 801 802 803
        {
            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];
            }
        }
    }
804 805 806

    /* */
    vlc_mutex_init( &p_owner->lock );
807 808
    vlc_cond_init( &p_owner->wait_request );
    vlc_cond_init( &p_owner->wait_acknowledge );
809

810 811
    p_owner->b_fmt_description = false;
    es_format_Init( &p_owner->fmt_description, UNKNOWN_ES, 0 );
Laurent Aimar's avatar
Laurent Aimar committed
812
    p_owner->p_description = NULL;
813

814
    p_owner->b_paused = false;
815
    p_owner->pause.i_date = VLC_TS_INVALID;
816
    p_owner->pause.i_ignore = 0;
817 818

    p_owner->b_buffering = false;
819
    p_owner->buffer.b_first = true;
820 821 822 823 824
    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;
825
    p_owner->buffer.p_block = NULL;
826 827 828

    p_owner->b_flushing = false;

829
    /* */
830
    p_owner->cc.b_supported = false;
831
    if( !b_packetizer )
832 833
    {
        if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
834
            p_owner->cc.b_supported = true;
835
        if( p_dec->pf_get_cc )
836
            p_owner->cc.b_supported = true;
837 838
    }

839
    for( unsigned i = 0; i < 4; i++ )
840
    {
841 842
        p_owner->cc.pb_present[i] = false;
        p_owner->cc.pp_decoder[i] = NULL;
843
    }
844
    p_owner->i_ts_delay = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
845 846 847
    return p_dec;
}

Clément Stenac's avatar
Clément Stenac committed
848 849 850 851 852
/**
 * The decoding main loop
 *
 * \param p_dec the decoder
 */
853
static void *DecoderThread( vlc_object_t *p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
854
{
855 856 857
    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
858
    /* The decoder's main loop */
859
    for( ;; )
860
    {
861
        block_t *p_block = block_FifoGet( p_owner->p_fifo );
862

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

867
        if( p_block )
868
        {
869 870
            int canc = vlc_savecancel();

871
            if( p_dec->b_error )
872
                DecoderError( p_dec, p_block );
873 874
            else
                DecoderProcess( p_dec, p_block );
875 876

            vlc_restorecancel( canc );
877
        }
878
    }
879
    return NULL;
880
}
Gildas Bazin's avatar
 
Gildas Bazin committed
881

882 883 884 885 886 887 888 889 890 891 892 893 894 895
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
896 897 898 899
static void DecoderFlush( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_owner = p_dec->p_owner;

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

902 903
    /* Empty the fifo */
    block_FifoEmpty( p_owner->p_fifo );
Laurent Aimar's avatar
Laurent Aimar committed
904

905 906
    /* Monitor for flush end */
    p_owner->b_flushing = true;
907
    vlc_cond_signal( &p_owner->wait_request );
Laurent Aimar's avatar
Laurent Aimar committed
908 909

    /* Send a special block */
910
    block_t *p_null = DecoderBlockFlushNew();
Laurent Aimar's avatar
Laurent Aimar committed
911 912
    if( !p_null )
        return;
913
    input_DecoderDecode( p_dec, p_null, false );
Laurent Aimar's avatar
Laurent Aimar committed
914 915

    /* */
916
    while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
917
        vlc_cond_wait( &p_owner->wait_acknowledge, &p_owner->lock );
Laurent Aimar's avatar
Laurent Aimar committed
918 919
}

920 921 922 923 924 925 926 927 928 929
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;
930
        vlc_cond_signal( &p_owner->wait_acknowledge );
931 932 933 934 935
    }

    vlc_mutex_unlock( &p_owner->lock );
}

Laurent Aimar's avatar
Laurent Aimar committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949
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;
}

950 951 952 953 954 955 956 957
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 )
    {
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        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;
        }
973
        vlc_cond_wait( &p_owner->wait_request, &p_owner->lock );
974 975 976 977 978 979
    }

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

Laurent Aimar's avatar
Laurent Aimar committed
980 981 982 983
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
984 985
    vlc_assert_locked( &p_owner->lock );

Laurent Aimar's avatar
Laurent Aimar committed
986 987 988 989
    /* XXX only audio and video output have to be paused.
     * - for sout it is useless
     * - for subs, it is done by the vout
     */
990
    if( p_dec->fmt_out.i_cat == AUDIO_ES )
Laurent Aimar's avatar
Laurent Aimar committed
991
    {
Laurent Aimar's avatar
Laurent Aimar committed
992 993 994
        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
995
    }
996
    else if( p_dec->fmt_out.i_cat == VIDEO_ES )
Laurent Aimar's avatar
Laurent Aimar committed
997 998 999 1000 1001
    {
        if( p_owner->p_vout )
            vout_ChangePause( p_owner->p_vout, b_paused, i_date );
    }
}
Laurent Aimar's avatar
Laurent Aimar committed
1002 1003
static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
{
1004 1005
    if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
        *pi_preroll = INT64_MAX;
1006
    else if( p->i_dts > VLC_TS_INVALID )
Laurent Aimar's avatar
Laurent Aimar committed
1007
        *pi_preroll = __MIN( *pi_preroll, p->i_dts );
1008
    else if( p->i_pts > VLC_TS_INVALID )
Laurent Aimar's avatar
Laurent Aimar committed
1009
        *pi_preroll = __MIN( *pi_preroll, p->i_pts );
Laurent Aimar's avatar
Laurent Aimar committed
1010
}
1011

1012
static mtime_t DecoderTeletextFixTs( mtime_t i_ts )
1013 1014 1015 1016
{
    mtime_t current_date = mdate();

    /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
1017
    if( i_ts <= VLC_TS_INVALID || i_ts > current_date + 10000000 || i_ts < current_date )
1018 1019 1020
    {
        /* ETSI EN 300 472 Annex A : do not take into account the PTS
         * for teletext streams. */
1021
        return current_date + 400000;
1022 1023 1024 1025
    }
    return i_ts;
}

Laurent Aimar's avatar
Laurent Aimar committed
1026
static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
1027
                          mtime_t *pi_duration, int *pi_rate, mtime_t i_ts_bound, bool b_telx )
1028
{
Laurent Aimar's avatar
Laurent Aimar committed
1029 1030
    decoder_owner_sys_t *p_owner = p_dec->p_owner;
    input_clock_t   *p_clock = p_owner->p_clock;
1031

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

Laurent Aimar's avatar
Laurent Aimar committed
1034
    const mtime_t i_es_delay = p_owner->i_ts_delay;
1035

Laurent Aimar's avatar
Laurent Aimar committed
1036 1037 1038
    if( p_clock )
    {
        const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
1039
        int i_rate;
1040

1041
        if( *pi_ts0 > VLC_TS_INVALID )