decoder.c 33.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
#include <stdlib.h>
30
#include <vlc/vlc.h>
31

Clément Stenac's avatar
Clément Stenac committed
32 33 34 35 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>

#include <vlc_interface.h>
#include "audio_output/aout_internal.h"
#include "stream_output/stream_output.h"
42
#include "input_internal.h"
43

44 45
static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
static void        DeleteDecoder( decoder_t * );
46

Gildas Bazin's avatar
 
Gildas Bazin committed
47
static int         DecoderThread( decoder_t * );
48
static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
Sam Hocevar's avatar
 
Sam Hocevar committed
49

Gildas Bazin's avatar
 
Gildas Bazin committed
50 51 52 53 54 55
/* 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
56 57
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
58

59 60 61
static subpicture_t *spu_new_buffer( decoder_t * );
static void spu_del_buffer( decoder_t *, subpicture_t * );

62
static es_format_t null_es_format;
Gildas Bazin's avatar
 
Gildas Bazin committed
63

64 65
struct decoder_owner_sys_t
{
66 67
    vlc_bool_t      b_own_thread;

Laurent Aimar's avatar
Laurent Aimar committed
68 69
    int64_t         i_preroll_end;

70 71
    input_thread_t  *p_input;

72 73 74 75 76
    aout_instance_t *p_aout;
    aout_input_t    *p_aout_input;

    vout_thread_t   *p_vout;

77 78 79
    vout_thread_t   *p_spu_vout;
    int              i_spu_channel;

80 81
    sout_instance_t         *p_sout;
    sout_packetizer_input_t *p_sout_input;
82

83 84 85
    /* Some decoders require already packetized data (ie. not truncated) */
    decoder_t *p_packetizer;

86 87 88 89 90 91 92 93 94 95
    /* Current format in use by the output */
    video_format_t video;
    audio_format_t audio;
    es_format_t    sout;

    /* fifo */
    block_fifo_t *p_fifo;
};


Clément Stenac's avatar
Clément Stenac committed
96 97 98 99 100 101 102
/**
 * Spawns a new decoder thread
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
 * \return the spawned decoder object
 */
103 104
decoder_t *input_DecoderNew( input_thread_t *p_input,
                             es_format_t *fmt, vlc_bool_t b_force_decoder )
105
{
106 107
    decoder_t   *p_dec = NULL;
    vlc_value_t val;
108

109
    /* If we are in sout mode, search for packetizer module */
Clément Stenac's avatar
Clément Stenac committed
110
    if( p_input->p->p_sout && !b_force_decoder )
111
    {
112
        /* Create the decoder configuration structure */
113
        p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
114
        if( p_dec == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
115
        {
116
            msg_Err( p_input, "could not create packetizer" );
117 118
            intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"), 
                            _("VLC could not open the packetizer module.") );
119
            return NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
120
        }
121
    }
122
    else
123
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
124
        /* Create the decoder configuration structure */
125
        p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
Gildas Bazin's avatar
 
Gildas Bazin committed
126 127 128
        if( p_dec == NULL )
        {
            msg_Err( p_input, "could not create decoder" );
129 130
            intf_UserFatal( p_input, VLC_FALSE, _("Streaming / Transcoding failed"), 
                            _("VLC could not open the decoder module.") );
Gildas Bazin's avatar
 
Gildas Bazin committed
131 132
            return NULL;
        }
133 134
    }

135
    if( !p_dec->p_module )
Sam Hocevar's avatar
 
Sam Hocevar committed
136
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
137 138
        msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
                 "VLC probably does not support this sound or video format.",
139
                 (char*)&p_dec->fmt_in.i_codec );
140
        intf_UserFatal( p_dec, VLC_FALSE, _("No suitable decoder module "
141
            "for format"), _("VLC probably does not support the \"%4.4s\" "
142 143
            "audio or video format. Unfortunately there is no way for you "
            "to fix this."), (char*)&p_dec->fmt_in.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
144 145 146

        DeleteDecoder( p_dec );
        vlc_object_destroy( p_dec );
147
        return NULL;
Henri Fallon's avatar
 
Henri Fallon committed
148 149
    }

Clément Stenac's avatar
Clément Stenac committed
150
    if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
151
        !b_force_decoder )
152 153 154 155 156 157 158 159 160
    {
        msg_Dbg( p_input, "stream out mode -> no decoder thread" );
        p_dec->p_owner->b_own_thread = VLC_FALSE;
    }
    else
    {
        var_Get( p_input, "minimize-threads", &val );
        p_dec->p_owner->b_own_thread = !val.b_bool;
    }
161

162
    if( p_dec->p_owner->b_own_thread )
Henri Fallon's avatar
 
Henri Fallon committed
163
    {
164
        int i_priority;
165
        if( fmt->i_cat == AUDIO_ES )
166 167 168 169 170 171 172 173
            i_priority = VLC_THREAD_PRIORITY_AUDIO;
        else
            i_priority = VLC_THREAD_PRIORITY_VIDEO;

        /* Spawn the decoder thread */
        if( vlc_thread_create( p_dec, "decoder", DecoderThread,
                               i_priority, VLC_FALSE ) )
        {
174
            msg_Err( p_dec, "cannot spawn decoder thread" );
175 176 177 178 179
            module_Unneed( p_dec, p_dec->p_module );
            DeleteDecoder( p_dec );
            vlc_object_destroy( p_dec );
            return NULL;
        }
Henri Fallon's avatar
 
Henri Fallon committed
180 181
    }

182
    return p_dec;
183 184
}

Clément Stenac's avatar
Clément Stenac committed
185 186 187 188 189 190 191
/**
 * Kills a decoder thread and waits until it's finished
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
 * \return nothing
 */
192
void input_DecoderDelete( decoder_t *p_dec )
193
{
194
    vlc_object_kill( p_dec );
195

196
    if( p_dec->p_owner->b_own_thread )
197
    {
198 199 200
        /* Make sure the thread leaves the function by
         * sending it an empty block. */
        block_t *p_block = block_New( p_dec, 0 );
201
        input_DecoderDecode( p_dec, p_block );
202

203
        vlc_thread_join( p_dec );
204 205 206

        /* Don't module_Unneed() here because of the dll loader that wants
         * close() in the same thread than open()/decode() */
207 208 209
    }
    else
    {
210 211 212
        /* Flush */
        input_DecoderDecode( p_dec, NULL );

213 214
        module_Unneed( p_dec, p_dec->p_module );
    }
215

Gildas Bazin's avatar
 
Gildas Bazin committed
216 217 218 219 220
    /* Delete decoder configuration */
    DeleteDecoder( p_dec );

    /* Delete the decoder */
    vlc_object_destroy( p_dec );
221
}
Clément Stenac's avatar
Clément Stenac committed
222 223

/**
224
 * Put a block_t in the decoder's fifo.
Clément Stenac's avatar
Clément Stenac committed
225 226 227 228
 *
 * \param p_dec the decoder object
 * \param p_block the data block
 */
229
void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
230
{
231 232
    if( p_dec->p_owner->b_own_thread )
    {
Clément Stenac's avatar
Clément Stenac committed
233
        if( p_dec->p_owner->p_input->p->b_out_pace_control )
234 235
        {
            /* FIXME !!!!! */
236 237
            while( !p_dec->b_die && !p_dec->b_error &&
                   p_dec->p_owner->p_fifo->i_depth > 10 )
238 239 240 241
            {
                msleep( 1000 );
            }
        }
242 243 244 245 246
        else if( p_dec->p_owner->p_fifo->i_size > 50000000 /* 50 MB */ )
        {
            /* FIXME: ideally we would check the time amount of data
             * in the fifo instead of its size. */
            msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
247
                      "consumed quickly enough), resetting fifo!" );
248 249 250 251
            block_FifoEmpty( p_dec->p_owner->p_fifo );
        }

        block_FifoPut( p_dec->p_owner->p_fifo, p_block );
252 253 254
    }
    else
    {
255
        if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
256
        {
257
            if( p_block ) block_Release( p_block );
258 259 260 261 262 263
        }
        else
        {
            DecoderDecode( p_dec, p_block );
        }
    }
264
}
265

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
void input_DecoderDiscontinuity( decoder_t * p_dec )
{
    block_t *p_null;

    /* Empty the fifo */
    if( p_dec->p_owner->b_own_thread )
    {
        block_FifoEmpty( p_dec->p_owner->p_fifo );
    }

    /* Send a special block */
    p_null = block_New( p_dec, 128 );
    p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
    memset( p_null->p_buffer, 0, p_null->i_buffer );

    input_DecoderDecode( p_dec, p_null );
}

Laurent Aimar's avatar
 
Laurent Aimar committed
284 285 286 287 288 289 290 291 292
vlc_bool_t input_DecoderEmpty( decoder_t * p_dec )
{
    if( p_dec->p_owner->b_own_thread && p_dec->p_owner->p_fifo->i_depth > 0 )
    {
        return VLC_FALSE;
    }
    return VLC_TRUE;
}

Laurent Aimar's avatar
Laurent Aimar committed
293 294 295 296
void input_DecoderPreroll( decoder_t *p_dec, int64_t i_preroll_end )
{
    p_dec->p_owner->i_preroll_end = i_preroll_end;
}
Clément Stenac's avatar
Clément Stenac committed
297 298 299 300 301 302 303 304
/**
 * Create a decoder object
 *
 * \param p_input the input thread
 * \param p_es the es descriptor
 * \param i_object_type Object type as define in include/vlc_objects.h
 * \return the decoder object
 */
305 306
static decoder_t * CreateDecoder( input_thread_t *p_input,
                                  es_format_t *fmt, int i_object_type )
Sam Hocevar's avatar
 
Sam Hocevar committed
307
{
Gildas Bazin's avatar
 
Gildas Bazin committed
308
    decoder_t *p_dec;
Sam Hocevar's avatar
 
Sam Hocevar committed
309

Gildas Bazin's avatar
 
Gildas Bazin committed
310
    p_dec = vlc_object_create( p_input, i_object_type );
Gildas Bazin's avatar
 
Gildas Bazin committed
311
    if( p_dec == NULL )
Sam Hocevar's avatar
 
Sam Hocevar committed
312
    {
313
        msg_Err( p_input, "out of memory" );
Sam Hocevar's avatar
 
Sam Hocevar committed
314 315 316
        return NULL;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
317 318 319 320
    p_dec->pf_decode_audio = 0;
    p_dec->pf_decode_video = 0;
    p_dec->pf_decode_sub = 0;
    p_dec->pf_packetize = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
321

322
   /* Initialize the decoder fifo */
Gildas Bazin's avatar
 
Gildas Bazin committed
323 324
    p_dec->p_module = NULL;

325
    memset( &null_es_format, 0, sizeof(es_format_t) );
326 327
    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
328 329

    /* Allocate our private structure for the decoder */
330
    p_dec->p_owner = malloc( sizeof( decoder_owner_sys_t ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
331 332 333 334 335
    if( p_dec->p_owner == NULL )
    {
        msg_Err( p_dec, "out of memory" );
        return NULL;
    }
336
    p_dec->p_owner->b_own_thread = VLC_TRUE;
Laurent Aimar's avatar
Laurent Aimar committed
337
    p_dec->p_owner->i_preroll_end = -1;
338
    p_dec->p_owner->p_input = p_input;
Gildas Bazin's avatar
 
Gildas Bazin committed
339 340 341
    p_dec->p_owner->p_aout = NULL;
    p_dec->p_owner->p_aout_input = NULL;
    p_dec->p_owner->p_vout = NULL;
342 343
    p_dec->p_owner->p_spu_vout = NULL;
    p_dec->p_owner->i_spu_channel = 0;
Clément Stenac's avatar
Clément Stenac committed
344
    p_dec->p_owner->p_sout = p_input->p->p_sout;
345
    p_dec->p_owner->p_sout_input = NULL;
346
    p_dec->p_owner->p_packetizer = NULL;
347

348 349 350 351 352 353
    /* decoder fifo */
    if( ( p_dec->p_owner->p_fifo = block_FifoNew( p_dec ) ) == NULL )
    {
        msg_Err( p_dec, "out of memory" );
        return NULL;
    }
354

Gildas Bazin's avatar
 
Gildas Bazin committed
355 356 357 358 359
    /* 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
360 361
    p_dec->pf_picture_link    = vout_link_picture;
    p_dec->pf_picture_unlink  = vout_unlink_picture;
362 363
    p_dec->pf_spu_buffer_new  = spu_new_buffer;
    p_dec->pf_spu_buffer_del  = spu_del_buffer;
Gildas Bazin's avatar
 
Gildas Bazin committed
364

Gildas Bazin's avatar
 
Gildas Bazin committed
365 366
    vlc_object_attach( p_dec, p_input );

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    /* Find a suitable decoder/packetizer module */
    if( i_object_type == VLC_OBJECT_DECODER )
        p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
    else
        p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );

    /* Check if decoder requires already packetized data */
    if( i_object_type == VLC_OBJECT_DECODER &&
        p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
    {
        p_dec->p_owner->p_packetizer =
            vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
        if( p_dec->p_owner->p_packetizer )
        {
            es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
                            &p_dec->fmt_in );

384 385 386
            es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
                            &null_es_format );

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
            vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );

            p_dec->p_owner->p_packetizer->p_module =
                module_Need( p_dec->p_owner->p_packetizer,
                             "packetizer", "$packetizer", 0 );

            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 );
                vlc_object_destroy( p_dec->p_owner->p_packetizer );
            }
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
402 403 404
    return p_dec;
}

Clément Stenac's avatar
Clément Stenac committed
405 406 407 408 409 410
/**
 * The decoding main loop
 *
 * \param p_dec the decoder
 * \return 0
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
411 412
static int DecoderThread( decoder_t * p_dec )
{
413
    block_t *p_block;
Gildas Bazin's avatar
 
Gildas Bazin committed
414 415

    /* The decoder's main loop */
416
    while( !p_dec->b_die && !p_dec->b_error )
Gildas Bazin's avatar
 
Gildas Bazin committed
417
    {
418
        if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
419
        {
420
            p_dec->b_error = 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
421 422
            break;
        }
423
        if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
424 425 426 427
        {
            break;
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
428

429 430 431 432
    while( !p_dec->b_die )
    {
        /* Trash all received PES packets */
        p_block = block_FifoGet( p_dec->p_owner->p_fifo );
433
        if( p_block ) block_Release( p_block );
434
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
435

436 437
    /* We do it here because of the dll loader that wants close() in the
     * same thread than open()/decode() */
438
    module_Unneed( p_dec, p_dec->p_module );
Gildas Bazin's avatar
 
Gildas Bazin committed
439

440 441
    return 0;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
442

Clément Stenac's avatar
Clément Stenac committed
443 444 445 446 447 448 449
/**
 * Decode a block
 *
 * \param p_dec the decoder object
 * \param p_block the block to decode
 * \return VLC_SUCCESS or an error code
 */
Laurent Aimar's avatar
Laurent Aimar committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
{
    if( p->i_pts > 0 )
        *pi_preroll = __MIN( *pi_preroll, p->i_pts );
    else if( p->i_dts > 0 )
        *pi_preroll = __MIN( *pi_preroll, p->i_dts );
}
static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
{
    input_thread_t *p_input = p_dec->p_owner->p_input;
    aout_buffer_t *p_aout_buf;

    //DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
    while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
    {
        vlc_mutex_lock( &p_input->p->counters.counters_lock );
        stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
        vlc_mutex_unlock( &p_input->p->counters.counters_lock );

        /* FIXME the best would be to handle the case
         * start_date < preroll < end_date
         * but that's not easy with non raw audio stream */
//        if( p_dec->p_owner->i_preroll_end > 0 )
//            msg_Err( p_dec, "Prerolling %lld - %lld", p_dec->p_owner->i_preroll_end, p_aout_buf->start_date );

        if( p_dec->p_owner->i_preroll_end > 0 &&
            p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
        {
            aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
                                  p_dec->p_owner->p_aout_input, p_aout_buf );
        }
        else
        {
            p_dec->p_owner->i_preroll_end = -1;
            aout_DecPlay( p_dec->p_owner->p_aout,
                          p_dec->p_owner->p_aout_input,
                          p_aout_buf );
        }
    }
}
static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
{
    input_thread_t *p_input = p_dec->p_owner->p_input;
    picture_t *p_pic;

    //DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
    while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
    {
        vlc_mutex_lock( &p_input->p->counters.counters_lock );
        stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
        vlc_mutex_unlock( &p_input->p->counters.counters_lock );

        if( p_dec->p_owner->i_preroll_end > 0 &&
            p_pic->date < p_dec->p_owner->i_preroll_end )
        {
            vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
        }
        else
        {
            p_dec->p_owner->i_preroll_end = -1;
            vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
                              p_pic->date );
            vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
        }
    }
}


518 519
static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
{
520
    int i_rate = p_block ? p_block->i_rate : 1000;
521

522
    if( p_block && p_block->i_buffer <= 0 )
523 524 525 526 527 528 529 530 531
    {
        block_Release( p_block );
        return VLC_SUCCESS;
    }

    if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
    {
        block_t *p_sout_block;

532 533
        while( ( p_sout_block =
                     p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
534
        {
535
            if( !p_dec->p_owner->p_sout_input )
536 537
            {
                es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
538

539
                p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
540
                p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
541 542
                if( p_dec->fmt_in.psz_language )
                {
543 544
                    if( p_dec->p_owner->sout.psz_language )
                        free( p_dec->p_owner->sout.psz_language );
545 546
                    p_dec->p_owner->sout.psz_language =
                        strdup( p_dec->fmt_in.psz_language );
547
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
548

549
                p_dec->p_owner->p_sout_input =
550 551
                    sout_InputNew( p_dec->p_owner->p_sout,
                                   &p_dec->p_owner->sout );
552

553
                if( p_dec->p_owner->p_sout_input == NULL )
Gildas Bazin's avatar
 
Gildas Bazin committed
554
                {
555 556
                    msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
                             (char *)&p_dec->p_owner->sout.i_codec );
557
                    p_dec->b_error = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
558

559
                    while( p_sout_block )
560
                    {
561
                        block_t *p_next = p_sout_block->p_next;
562 563
                        block_Release( p_sout_block );
                        p_sout_block = p_next;
564
                    }
565 566 567
                    break;
                }
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
568

569 570
            while( p_sout_block )
            {
571
                block_t *p_next = p_sout_block->p_next;
Gildas Bazin's avatar
 
Gildas Bazin committed
572

573
                p_sout_block->p_next = NULL;
574
                p_sout_block->i_rate = i_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
575

576
                sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
577
                                      p_sout_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
578

579
                p_sout_block = p_next;
Gildas Bazin's avatar
 
Gildas Bazin committed
580
            }
581 582 583

            /* For now it's enough, as only sout inpact on this flag */
            if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
Clément Stenac's avatar
Clément Stenac committed
584
                p_dec->p_owner->p_input->p->b_out_pace_control )
585
            {
586
                msg_Dbg( p_dec, "switching to sync mode" );
Clément Stenac's avatar
Clément Stenac committed
587
                p_dec->p_owner->p_input->p->b_out_pace_control = VLC_FALSE;
588 589
            }
            else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
Clément Stenac's avatar
Clément Stenac committed
590
                     !p_dec->p_owner->p_input->p->b_out_pace_control )
591
            {
592
                msg_Dbg( p_dec, "switching to async mode" );
Clément Stenac's avatar
Clément Stenac committed
593
                p_dec->p_owner->p_input->p->b_out_pace_control = VLC_TRUE;
594
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
595
        }
596 597 598
    }
    else if( p_dec->fmt_in.i_cat == AUDIO_ES )
    {
599 600 601 602 603 604 605 606
        if( p_dec->p_owner->p_packetizer )
        {
            block_t *p_packetized_block;
            decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;

            while( (p_packetized_block =
                    p_packetizer->pf_packetize( p_packetizer, &p_block )) )
            {
607 608
                if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
                {
609 610
                    es_format_Clean( &p_dec->fmt_in );
                    es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
611 612
                }

613
                while( p_packetized_block )
614
                {
615 616
                    block_t *p_next = p_packetized_block->p_next;
                    p_packetized_block->p_next = NULL;
617
                    p_packetized_block->i_rate = i_rate;
618

Laurent Aimar's avatar
Laurent Aimar committed
619
                    DecoderDecodeAudio( p_dec, p_packetized_block );
620 621

                    p_packetized_block = p_next;
622 623 624
                }
            }
        }
Laurent Aimar's avatar
Laurent Aimar committed
625
        else
626
        {
Laurent Aimar's avatar
Laurent Aimar committed
627
            DecoderDecodeAudio( p_dec, p_block );
628
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
629
    }
630
    else if( p_dec->fmt_in.i_cat == VIDEO_ES )
631
    {
632 633 634 635 636 637 638 639
        if( p_dec->p_owner->p_packetizer )
        {
            block_t *p_packetized_block;
            decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;

            while( (p_packetized_block =
                    p_packetizer->pf_packetize( p_packetizer, &p_block )) )
            {
640 641
                if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
                {
642 643
                    es_format_Clean( &p_dec->fmt_in );
                    es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
644 645
                }

646
                while( p_packetized_block )
647
                {
648 649
                    block_t *p_next = p_packetized_block->p_next;
                    p_packetized_block->p_next = NULL;
650
                    p_packetized_block->i_rate = i_rate;
651

Laurent Aimar's avatar
Laurent Aimar committed
652
                    DecoderDecodeVideo( p_dec, p_packetized_block );
653 654

                    p_packetized_block = p_next;
655 656 657
                }
            }
        }
Laurent Aimar's avatar
Laurent Aimar committed
658
        else
659
        {
Laurent Aimar's avatar
Laurent Aimar committed
660
            DecoderDecodeVideo( p_dec, p_block );
661
        }
662
    }
663 664
    else if( p_dec->fmt_in.i_cat == SPU_ES )
    {
Laurent Aimar's avatar
Laurent Aimar committed
665
        input_thread_t *p_input = p_dec->p_owner->p_input;
666 667
        vout_thread_t *p_vout;
        subpicture_t *p_spu;
Laurent Aimar's avatar
Laurent Aimar committed
668
        //DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
669 670
        while( (p_spu = p_dec->pf_decode_sub( p_dec, &p_block ) ) )
        {
Laurent Aimar's avatar
Laurent Aimar committed
671 672 673
            vlc_mutex_lock( &p_input->p->counters.counters_lock );
            stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
            vlc_mutex_unlock( &p_input->p->counters.counters_lock );
674

Laurent Aimar's avatar
Laurent Aimar committed
675 676 677 678 679 680 681 682 683
            if( p_dec->p_owner->i_preroll_end > 0 &&
                p_spu->i_start < p_dec->p_owner->i_preroll_end &&
                ( p_spu->i_stop <= 0 || p_spu->i_stop <= p_dec->p_owner->i_preroll_end ) )
            {
                spu_DestroySubpicture( p_dec->p_owner->p_vout->p_spu, p_spu );
                continue;
            }

            p_dec->p_owner->i_preroll_end = -1;
684 685 686
            p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
            if( p_vout )
            {
687
                spu_DisplaySubpicture( p_vout->p_spu, p_spu );
688 689 690
                vlc_object_release( p_vout );
            }
        }
691 692 693
    }
    else
    {
Clément Stenac's avatar
Clément Stenac committed
694
        msg_Err( p_dec, "unknown ES format" );
695 696
        p_dec->b_error = 1;
    }
697

698
    return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
Sam Hocevar's avatar
 
Sam Hocevar committed
699 700
}

Clément Stenac's avatar
Clément Stenac committed
701 702 703 704 705 706
/**
 * Destroys a decoder object
 *
 * \param p_dec the decoder object
 * \return nothing
 */
Gildas Bazin's avatar
 
Gildas Bazin committed
707
static void DeleteDecoder( decoder_t * p_dec )
Sam Hocevar's avatar
 
Sam Hocevar committed
708
{
709
    msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %d PES in FIFO",
710 711
             (char*)&p_dec->fmt_in.i_codec,
             p_dec->p_owner->p_fifo->i_depth );
712

Sam Hocevar's avatar
 
Sam Hocevar committed
713
    /* Free all packets still in the decoder fifo. */
714 715
    block_FifoEmpty( p_dec->p_owner->p_fifo );
    block_FifoRelease( p_dec->p_owner->p_fifo );
Sam Hocevar's avatar
 
Sam Hocevar committed
716

717
    /* Cleanup */
Gildas Bazin's avatar
 
Gildas Bazin committed
718 719 720 721 722 723 724
    if( p_dec->p_owner->p_aout_input )
        aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );

    if( p_dec->p_owner->p_vout )
    {
        int i_pic;

Gildas Bazin's avatar
 
Gildas Bazin committed
725
#define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
Gildas Bazin's avatar
 
Gildas Bazin committed
726 727 728 729
        /* Hack to make sure all the the pictures are freed by the decoder */
        for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
             i_pic++ )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
730 731 732 733
            if( p_pic->i_status == RESERVED_PICTURE )
                vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
            if( p_pic->i_refcount > 0 )
                vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
Gildas Bazin's avatar
 
Gildas Bazin committed
734
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
735
#undef p_pic
Gildas Bazin's avatar
 
Gildas Bazin committed
736 737

        /* We are about to die. Reattach video output to p_vlc. */
738
        vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
739 740
    }

741
    if( p_dec->p_owner->p_sout_input )
Gildas Bazin's avatar
 
Gildas Bazin committed
742
    {
743
        sout_InputDelete( p_dec->p_owner->p_sout_input );
744
        es_format_Clean( &p_dec->p_owner->sout );
Gildas Bazin's avatar
 
Gildas Bazin committed
745 746
    }

747 748 749 750 751 752 753
    if( p_dec->fmt_in.i_cat == SPU_ES )
    {
        vout_thread_t *p_vout;

        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
        if( p_vout )
        {
754 755
            spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
                         p_dec->p_owner->i_spu_channel );
756 757 758 759
            vlc_object_release( p_vout );
        }
    }

760 761 762 763 764 765 766 767 768 769 770 771
    es_format_Clean( &p_dec->fmt_in );
    es_format_Clean( &p_dec->fmt_out );

    if( p_dec->p_owner->p_packetizer )
    {
        module_Unneed( p_dec->p_owner->p_packetizer,
                       p_dec->p_owner->p_packetizer->p_module );
        es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
        es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
        vlc_object_detach( p_dec->p_owner->p_packetizer );
        vlc_object_destroy( p_dec->p_owner->p_packetizer );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
772

773 774
    vlc_object_detach( p_dec );

Gildas Bazin's avatar
 
Gildas Bazin committed
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
    free( p_dec->p_owner );
}

/*****************************************************************************
 * Buffers allocation callbacks for the decoders
 *****************************************************************************/
static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
{
    decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
    aout_buffer_t *p_buffer;

    if( p_sys->p_aout_input != NULL &&
        ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
          p_dec->fmt_out.audio.i_original_channels !=
              p_sys->audio.i_original_channels ||
          p_dec->fmt_out.audio.i_bytes_per_frame !=
              p_sys->audio.i_bytes_per_frame ) )
    {
        /* Parameters changed, restart the aout */
        aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
        p_sys->p_aout_input = NULL;
    }

    if( p_sys->p_aout_input == NULL )
    {
800 801 802
        audio_sample_format_t format;
        int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );

Gildas Bazin's avatar
 
Gildas Bazin committed
803 804
        p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
        p_sys->audio = p_dec->fmt_out.audio;
805 806 807 808 809

        memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
        if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
                                    == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
        {
810
            if ( i_force_dolby == 1 )
811 812 813 814
            {
                format.i_original_channels = format.i_original_channels |
                                             AOUT_CHAN_DOLBYSTEREO;
            }
815
            else /* i_force_dolby == 2 */
816 817 818 819 820 821
            {
                format.i_original_channels = format.i_original_channels &
                                             ~AOUT_CHAN_DOLBYSTEREO;
            }
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
822
        p_sys->p_aout_input =
823
            aout_DecNew( p_dec, &p_sys->p_aout, &format );
Gildas Bazin's avatar
 
Gildas Bazin committed
824 825 826
        if( p_sys->p_aout_input == NULL )
        {
            msg_Err( p_dec, "failed to create audio output" );
Gildas Bazin's avatar
 
Gildas Bazin committed
827
            p_dec->b_error = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
            return NULL;
        }
        p_dec->fmt_out.audio.i_bytes_per_frame =
            p_sys->audio.i_bytes_per_frame;
    }

    p_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
                                  i_samples );

    return p_buffer;
}

static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
{
    aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
                          p_dec->p_owner->p_aout_input, p_buffer );
}

static picture_t *vout_new_buffer( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
    picture_t *p_pic;

    if( p_sys->p_vout == NULL ||
        p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
        p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
        p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
        p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
    {
        if( !p_dec->fmt_out.video.i_width ||
            !p_dec->fmt_out.video.i_height )
        {
            /* Can't create a new vout without display size */
            return NULL;
        }

864 865 866
        if( !p_dec->fmt_out.video.i_visible_width ||
            !p_dec->fmt_out.video.i_visible_height )
        {
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
            if( p_dec->fmt_in.video.i_visible_width &&
                p_dec->fmt_in.video.i_visible_height )
            {
                p_dec->fmt_out.video.i_visible_width =
                    p_dec->fmt_in.video.i_visible_width;
                p_dec->fmt_out.video.i_visible_height =
                    p_dec->fmt_in.video.i_visible_height;
            }
            else
            {
                p_dec->fmt_out.video.i_visible_width =
                    p_dec->fmt_out.video.i_width;
                p_dec->fmt_out.video.i_visible_height =
                    p_dec->fmt_out.video.i_height;
            }
882 883
        }

884 885
        if( p_dec->fmt_out.video.i_visible_height == 1088 &&
            var_CreateGetBool( p_dec, "hdtv-fix" ) )
886
        {
887
            p_dec->fmt_out.video.i_visible_height = 1080;
888 889
            p_dec->fmt_out.video.i_sar_num *= 135;
            p_dec->fmt_out.video.i_sar_den *= 136;
890
            msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
891 892
        }

893 894 895
        if( !p_dec->fmt_out.video.i_sar_num ||
            !p_dec->fmt_out.video.i_sar_den )
        {
896
            p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
897
              p_dec->fmt_out.video.i_visible_height;
898 899

            p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
900
              p_dec->fmt_out.video.i_visible_width;
901 902
        }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
903 904 905
        vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
                     &p_dec->fmt_out.video.i_sar_den,
                     p_dec->fmt_out.video.i_sar_num,
906
                     p_dec->fmt_out.video.i_sar_den, 50000 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
907

Gildas Bazin's avatar
 
Gildas Bazin committed
908 909 910 911
        p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
        p_sys->video = p_dec->fmt_out.video;

        p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
912
                                      &p_dec->fmt_out.video );
Gildas Bazin's avatar
 
Gildas Bazin committed
913 914 915
        if( p_sys->p_vout == NULL )
        {
            msg_Err( p_dec, "failed to create video output" );
Gildas Bazin's avatar
 
Gildas Bazin committed
916
            p_dec->b_error = VLC_TRUE;
Gildas Bazin's avatar
 
Gildas Bazin committed
917 918
            return NULL;
        }
919 920 921 922 923 924 925

        if( p_sys->video.i_rmask )
            p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
        if( p_sys->video.i_gmask )
            p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
        if( p_sys->video.i_bmask )
            p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
Gildas Bazin's avatar
 
Gildas Bazin committed
926 927 928 929 930
    }

    /* Get a new picture */
    while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
931
        int i_pic, i_ready_pic = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
932

Gildas Bazin's avatar
 
Gildas Bazin committed
933 934 935 936
        if( p_dec->b_die || p_dec->b_error )
        {
            return NULL;
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
937 938 939 940 941 942

#define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
        /* Check the decoder doesn't leak pictures */
        for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
             i_pic++ )
        {
943 944 945 946 947
            if( p_pic->i_status == READY_PICTURE )
            {
                if( i_ready_pic++ > 0 ) break;
                else continue;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
948

Gildas Bazin's avatar
 
Gildas Bazin committed
949
            if( p_pic->i_status != DISPLAYED_PICTURE &&
Gildas Bazin's avatar
 
Gildas Bazin committed
950 951
                p_pic->i_status != RESERVED_PICTURE &&
                p_pic->i_status != READY_PICTURE ) break;
Gildas Bazin's avatar
 
Gildas Bazin committed
952

953 954
            if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
                break;
Gildas Bazin's avatar
 
Gildas Bazin committed
955 956 957
        }
        if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
        {
Clément Stenac's avatar
Clément Stenac committed
958
            msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
Gildas Bazin's avatar
 
Gildas Bazin committed
959 960 961 962 963 964 965 966 967 968 969 970 971

            /* Just free all the pictures */
            for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
                 i_pic++ )
            {
                if( p_pic->i_status == RESERVED_PICTURE )
                    vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
                if( p_pic->i_refcount > 0 )
                vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
            }
        }
#undef p_pic

Gildas Bazin's avatar
 
Gildas Bazin committed
972 973 974 975 976 977 978 979 980
        msleep( VOUT_OUTMEM_SLEEP );
    }

    return p_pic;
}

static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
{
    vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
Gildas Bazin's avatar
 
Gildas Bazin committed
981
}
Gildas Bazin's avatar
 
Gildas Bazin committed
982 983 984 985 986 987 988 989 990 991

static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
{
    vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
}

static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
{
    vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
}
992 993 994 995

static subpicture_t *spu_new_buffer( decoder_t *p_dec )
{
    decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
996
    vout_thread_t *p_vout = NULL;
997
    subpicture_t *p_subpic;
998
    int i_attempts = 30;
999

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    while( i_attempts-- )
    {
        if( p_dec->b_die || p_dec->b_error ) break;

        p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
        if( p_vout ) break;

        msleep( VOUT_DISPLAY_DELAY );
    }

    if( !p_vout )
    {
        msg_Warn( p_dec, "no vout found, dropping subpicture" );
        return NULL;
    }
1015 1016 1017

    if( p_sys->p_spu_vout != p_vout )
    {
1018 1019
        spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
                     &p_sys->i_spu_channel );
1020 1021 1022
        p_sys->p_spu_vout = p_vout;
    }

1023
    p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1024 1025 1026 1027
    if( p_subpic )
    {
        p_subpic->i_channel = p_sys->i_spu_channel;
    }
1028 1029 1030

    vlc_object_release( p_vout );

1031
    return p_subpic;
1032 1033
}

1034
static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1035
{
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
    decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
    vout_thread_t *p_vout = NULL;

    p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
    if( !p_vout || p_sys->p_spu_vout != p_vout )
    {
        if( p_vout )
            vlc_object_release( p_vout );
        msg_Warn( p_dec, "no vout found, leaking subpicture" );
        return;
    }

    spu_DestroySubpicture( p_vout->p_spu, p_subpic );

    vlc_object_release( p_vout );
1051
}
1052