]> git.sesse.net Git - vlc/blob - src/input/decoder.c
libvlc: use vlc_common.h (libvlccore) instead of vlc/vlc.h
[vlc] / src / input / decoder.c
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  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
47
48 static decoder_t * CreateDecoder( input_thread_t *, es_format_t *, int );
49 static void        DeleteDecoder( decoder_t * );
50
51 static int         DecoderThread( decoder_t * );
52 static int         DecoderDecode( decoder_t * p_dec, block_t *p_block );
53
54 /* Buffers allocation callbacks for the decoders */
55 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
56 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
57
58 static picture_t *vout_new_buffer( decoder_t * );
59 static void vout_del_buffer( decoder_t *, picture_t * );
60 static void vout_link_picture( decoder_t *, picture_t * );
61 static void vout_unlink_picture( decoder_t *, picture_t * );
62
63 static subpicture_t *spu_new_buffer( decoder_t * );
64 static void spu_del_buffer( decoder_t *, subpicture_t * );
65
66 static es_format_t null_es_format;
67
68 struct decoder_owner_sys_t
69 {
70     bool      b_own_thread;
71
72     int64_t         i_preroll_end;
73
74     input_thread_t  *p_input;
75
76     aout_instance_t *p_aout;
77     aout_input_t    *p_aout_input;
78
79     vout_thread_t   *p_vout;
80
81     vout_thread_t   *p_spu_vout;
82     int              i_spu_channel;
83
84     sout_instance_t         *p_sout;
85     sout_packetizer_input_t *p_sout_input;
86
87     /* Some decoders require already packetized data (ie. not truncated) */
88     decoder_t *p_packetizer;
89
90     /* Current format in use by the output */
91     video_format_t video;
92     audio_format_t audio;
93     es_format_t    sout;
94
95     /* fifo */
96     block_fifo_t *p_fifo;
97
98     /* CC */
99     bool b_cc_supported;
100     vlc_mutex_t lock_cc;
101     bool pb_cc_present[4];
102     decoder_t *pp_cc[4];
103 };
104
105 /* */
106 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
107 {
108     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
109              "VLC probably does not support this sound or video format.",
110              (char*)&codec );
111     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
112                     _("VLC does not support the audio or video format \"%4.4s\". "
113                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
114 }
115
116 /* decoder_GetInputAttachment:
117  */
118 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
119                                                 const char *psz_name )
120 {
121     input_attachment_t *p_attachment;
122     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
123         return NULL;
124     return p_attachment;
125 }
126 /* decoder_GetInputAttachments:
127  */
128 int decoder_GetInputAttachments( decoder_t *p_dec,
129                                  input_attachment_t ***ppp_attachment,
130                                  int *pi_attachment )
131 {
132     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
133                           ppp_attachment, pi_attachment );
134 }
135 /* decoder_GetDisplayDate:
136  */
137 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
138 {
139     VLC_UNUSED(p_dec);
140     return i_ts;
141 }
142
143 /**
144  * Spawns a new decoder thread
145  *
146  * \param p_input the input thread
147  * \param p_es the es descriptor
148  * \return the spawned decoder object
149  */
150 decoder_t *input_DecoderNew( input_thread_t *p_input,
151                              es_format_t *fmt, bool b_force_decoder )
152 {
153     decoder_t   *p_dec = NULL;
154     vlc_value_t val;
155
156 #ifndef ENABLE_SOUT
157     (void)b_force_decoder;
158 #else
159     /* If we are in sout mode, search for packetizer module */
160     if( p_input->p->p_sout && !b_force_decoder )
161     {
162         /* Create the decoder configuration structure */
163         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER );
164         if( p_dec == NULL )
165         {
166             msg_Err( p_input, "could not create packetizer" );
167             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
168                             _("VLC could not open the packetizer module.") );
169             return NULL;
170         }
171     }
172     else
173 #endif
174     {
175         /* Create the decoder configuration structure */
176         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER );
177         if( p_dec == NULL )
178         {
179             msg_Err( p_input, "could not create decoder" );
180             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
181                             _("VLC could not open the decoder module.") );
182             return NULL;
183         }
184     }
185
186     if( !p_dec->p_module )
187     {
188         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
189
190         DeleteDecoder( p_dec );
191         vlc_object_release( p_dec );
192         return NULL;
193     }
194
195     if( p_input->p->p_sout && p_input->p->input.b_can_pace_control &&
196         !b_force_decoder )
197     {
198         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
199         p_dec->p_owner->b_own_thread = false;
200     }
201     else
202     {
203         var_Get( p_input, "minimize-threads", &val );
204         p_dec->p_owner->b_own_thread = !val.b_bool;
205     }
206
207     if( p_dec->p_owner->b_own_thread )
208     {
209         int i_priority;
210         if( fmt->i_cat == AUDIO_ES )
211             i_priority = VLC_THREAD_PRIORITY_AUDIO;
212         else
213             i_priority = VLC_THREAD_PRIORITY_VIDEO;
214
215         /* Spawn the decoder thread */
216         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
217                                i_priority, false ) )
218         {
219             msg_Err( p_dec, "cannot spawn decoder thread" );
220             module_Unneed( p_dec, p_dec->p_module );
221             DeleteDecoder( p_dec );
222             vlc_object_release( p_dec );
223             return NULL;
224         }
225     }
226
227     return p_dec;
228 }
229
230 /**
231  * Kills a decoder thread and waits until it's finished
232  *
233  * \param p_input the input thread
234  * \param p_es the es descriptor
235  * \return nothing
236  */
237 void input_DecoderDelete( decoder_t *p_dec )
238 {
239     vlc_object_kill( p_dec );
240
241     if( p_dec->p_owner->b_own_thread )
242     {
243         /* Make sure the thread leaves the function by
244          * sending it an empty block. */
245         block_t *p_block = block_New( p_dec, 0 );
246         input_DecoderDecode( p_dec, p_block );
247
248         vlc_thread_join( p_dec );
249
250         /* Don't module_Unneed() here because of the dll loader that wants
251          * close() in the same thread than open()/decode() */
252     }
253     else
254     {
255         /* Flush */
256         input_DecoderDecode( p_dec, NULL );
257
258         module_Unneed( p_dec, p_dec->p_module );
259     }
260
261     /* */
262     if( p_dec->p_owner->b_cc_supported )
263     {
264         int i;
265         for( i = 0; i < 4; i++ )
266             input_DecoderSetCcState( p_dec, false, i );
267     }
268
269     /* Delete decoder configuration */
270     DeleteDecoder( p_dec );
271
272     /* Delete the decoder */
273     vlc_object_release( p_dec );
274 }
275
276 /**
277  * Put a block_t in the decoder's fifo.
278  *
279  * \param p_dec the decoder object
280  * \param p_block the data block
281  */
282 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
283 {
284     if( p_dec->p_owner->b_own_thread )
285     {
286         if( p_dec->p_owner->p_input->p->b_out_pace_control )
287         {
288             /* FIXME !!!!! */
289             while( !p_dec->b_die && !p_dec->b_error &&
290                    block_FifoCount( p_dec->p_owner->p_fifo ) > 10 )
291             {
292                 msleep( 1000 );
293             }
294         }
295         else if( block_FifoSize( p_dec->p_owner->p_fifo ) > 50000000 /* 50 MB */ )
296         {
297             /* FIXME: ideally we would check the time amount of data
298              * in the fifo instead of its size. */
299             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
300                       "consumed quickly enough), resetting fifo!" );
301             block_FifoEmpty( p_dec->p_owner->p_fifo );
302         }
303
304         block_FifoPut( p_dec->p_owner->p_fifo, p_block );
305     }
306     else
307     {
308         if( p_dec->b_error || (p_block && p_block->i_buffer <= 0) )
309         {
310             if( p_block ) block_Release( p_block );
311         }
312         else
313         {
314             DecoderDecode( p_dec, p_block );
315         }
316     }
317 }
318
319 void input_DecoderDiscontinuity( decoder_t * p_dec, bool b_flush )
320 {
321     block_t *p_null;
322
323     /* Empty the fifo */
324     if( p_dec->p_owner->b_own_thread && b_flush )
325         block_FifoEmpty( p_dec->p_owner->p_fifo );
326
327     /* Send a special block */
328     p_null = block_New( p_dec, 128 );
329     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
330     /* FIXME check for p_packetizer or b_packitized from es_format_t of input ? */
331     if( p_dec->p_owner->p_packetizer && b_flush )
332         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
333     memset( p_null->p_buffer, 0, p_null->i_buffer );
334
335     input_DecoderDecode( p_dec, p_null );
336 }
337
338 bool input_DecoderEmpty( decoder_t * p_dec )
339 {
340     if( p_dec->p_owner->b_own_thread
341      && block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
342     {
343         return false;
344     }
345     return true;
346 }
347
348 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
349 {
350     int i;
351
352     vlc_mutex_lock( &p_dec->p_owner->lock_cc );
353     for( i = 0; i < 4; i++ )
354         pb_present[i] =  p_dec->p_owner->pb_cc_present[i];
355     vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
356 }
357 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
358 {
359     decoder_owner_sys_t *p_owner = p_dec->p_owner;
360
361     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
362
363     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
364         return VLC_EGENERIC;
365
366     if( b_decode )
367     {
368         static const vlc_fourcc_t fcc[4] = {
369             VLC_FOURCC('c', 'c', '1', ' '),
370             VLC_FOURCC('c', 'c', '2', ' '),
371             VLC_FOURCC('c', 'c', '3', ' '),
372             VLC_FOURCC('c', 'c', '4', ' '),
373         };
374         decoder_t *p_cc;
375         es_format_t fmt;
376
377         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
378         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER );
379         if( !p_cc )
380         {
381             msg_Err( p_dec, "could not create decoder" );
382             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
383                             _("VLC could not open the decoder module.") );
384             return VLC_EGENERIC;
385         }
386         else if( !p_cc->p_module )
387         {
388             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
389             DeleteDecoder( p_cc );
390             vlc_object_release( p_cc );
391             return VLC_EGENERIC;
392         }
393
394         vlc_mutex_lock( &p_owner->lock_cc );
395         p_dec->p_owner->pp_cc[i_channel] = p_cc;
396         vlc_mutex_unlock( &p_owner->lock_cc );
397     }
398     else
399     {
400         decoder_t *p_cc;
401
402         vlc_mutex_lock( &p_owner->lock_cc );
403         p_cc = p_dec->p_owner->pp_cc[i_channel];
404         p_dec->p_owner->pp_cc[i_channel] = NULL;
405         vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
406
407         if( p_cc )
408         {
409             vlc_object_kill( p_cc );
410             module_Unneed( p_cc, p_cc->p_module );
411             DeleteDecoder( p_cc );
412             vlc_object_release( p_cc );
413         }
414     }
415     return VLC_SUCCESS;
416 }
417 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
418 {
419     decoder_owner_sys_t *p_owner = p_dec->p_owner;
420
421     *pb_decode = false;
422     if( i_channel < 0 || i_channel >= 4 || !p_owner->pb_cc_present[i_channel] )
423         return VLC_EGENERIC;
424
425     vlc_mutex_lock( &p_owner->lock_cc );
426     *pb_decode = p_dec->p_owner->pp_cc[i_channel] != NULL;
427     vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
428     return VLC_EGENERIC;
429 }
430
431 /**
432  * Create a decoder object
433  *
434  * \param p_input the input thread
435  * \param p_es the es descriptor
436  * \param i_object_type Object type as define in include/vlc_objects.h
437  * \return the decoder object
438  */
439 static decoder_t * CreateDecoder( input_thread_t *p_input,
440                                   es_format_t *fmt, int i_object_type )
441 {
442     decoder_t *p_dec;
443     decoder_owner_sys_t *p_owner;
444     int i;
445
446     p_dec = vlc_object_create( p_input, i_object_type );
447     if( p_dec == NULL )
448     {
449         msg_Err( p_input, "out of memory" );
450         return NULL;
451     }
452
453     p_dec->pf_decode_audio = 0;
454     p_dec->pf_decode_video = 0;
455     p_dec->pf_decode_sub = 0;
456     p_dec->pf_get_cc = 0;
457     p_dec->pf_packetize = 0;
458
459     /* Initialize the decoder fifo */
460     p_dec->p_module = NULL;
461
462     memset( &null_es_format, 0, sizeof(es_format_t) );
463     es_format_Copy( &p_dec->fmt_in, fmt );
464     es_format_Copy( &p_dec->fmt_out, &null_es_format );
465
466     /* Allocate our private structure for the decoder */
467     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
468     if( p_dec->p_owner == NULL )
469     {
470         msg_Err( p_dec, "out of memory" );
471         return NULL;
472     }
473     p_dec->p_owner->b_own_thread = true;
474     p_dec->p_owner->i_preroll_end = -1;
475     p_dec->p_owner->p_input = p_input;
476     p_dec->p_owner->p_aout = NULL;
477     p_dec->p_owner->p_aout_input = NULL;
478     p_dec->p_owner->p_vout = NULL;
479     p_dec->p_owner->p_spu_vout = NULL;
480     p_dec->p_owner->i_spu_channel = 0;
481     p_dec->p_owner->p_sout = p_input->p->p_sout;
482     p_dec->p_owner->p_sout_input = NULL;
483     p_dec->p_owner->p_packetizer = NULL;
484
485     /* decoder fifo */
486     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
487     {
488         msg_Err( p_dec, "out of memory" );
489         return NULL;
490     }
491
492     /* Set buffers allocation callbacks for the decoders */
493     p_dec->pf_aout_buffer_new = aout_new_buffer;
494     p_dec->pf_aout_buffer_del = aout_del_buffer;
495     p_dec->pf_vout_buffer_new = vout_new_buffer;
496     p_dec->pf_vout_buffer_del = vout_del_buffer;
497     p_dec->pf_picture_link    = vout_link_picture;
498     p_dec->pf_picture_unlink  = vout_unlink_picture;
499     p_dec->pf_spu_buffer_new  = spu_new_buffer;
500     p_dec->pf_spu_buffer_del  = spu_del_buffer;
501
502     vlc_object_attach( p_dec, p_input );
503
504     /* Find a suitable decoder/packetizer module */
505     if( i_object_type == VLC_OBJECT_DECODER )
506         p_dec->p_module = module_Need( p_dec, "decoder", "$codec", 0 );
507     else
508         p_dec->p_module = module_Need( p_dec, "packetizer", "$packetizer", 0 );
509
510     /* Check if decoder requires already packetized data */
511     if( i_object_type == VLC_OBJECT_DECODER &&
512         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
513     {
514         p_dec->p_owner->p_packetizer =
515             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
516         if( p_dec->p_owner->p_packetizer )
517         {
518             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
519                             &p_dec->fmt_in );
520
521             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
522                             &null_es_format );
523
524             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
525
526             p_dec->p_owner->p_packetizer->p_module =
527                 module_Need( p_dec->p_owner->p_packetizer,
528                              "packetizer", "$packetizer", 0 );
529
530             if( !p_dec->p_owner->p_packetizer->p_module )
531             {
532                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
533                 vlc_object_detach( p_dec->p_owner->p_packetizer );
534                 vlc_object_release( p_dec->p_owner->p_packetizer );
535             }
536         }
537     }
538
539     /* Copy ourself the input replay gain */
540     if( fmt->i_cat == AUDIO_ES )
541     {
542         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
543         {
544             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
545             {
546                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
547                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
548             }
549             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
550             {
551                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
552                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
553             }
554         }
555     }
556     /* */
557     p_owner->b_cc_supported = false;
558     if( i_object_type == VLC_OBJECT_DECODER )
559     {
560         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
561             p_owner->b_cc_supported = true;
562         if( p_dec->pf_get_cc )
563             p_owner->b_cc_supported = true;
564     }
565
566     vlc_mutex_init( &p_owner->lock_cc );
567     for( i = 0; i < 4; i++ )
568     {
569         p_owner->pb_cc_present[i] = false;
570         p_owner->pp_cc[i] = NULL;
571     }
572     return p_dec;
573 }
574
575 /**
576  * The decoding main loop
577  *
578  * \param p_dec the decoder
579  * \return 0
580  */
581 static int DecoderThread( decoder_t * p_dec )
582 {
583     block_t *p_block;
584
585     /* The decoder's main loop */
586     while( !p_dec->b_die && !p_dec->b_error )
587     {
588         if( ( p_block = block_FifoGet( p_dec->p_owner->p_fifo ) ) == NULL )
589         {
590             p_dec->b_error = 1;
591             break;
592         }
593         if( DecoderDecode( p_dec, p_block ) != VLC_SUCCESS )
594         {
595             break;
596         }
597     }
598
599     while( !p_dec->b_die )
600     {
601         /* Trash all received PES packets */
602         p_block = block_FifoGet( p_dec->p_owner->p_fifo );
603         if( p_block ) block_Release( p_block );
604     }
605
606     /* We do it here because of the dll loader that wants close() in the
607      * same thread than open()/decode() */
608     module_Unneed( p_dec, p_dec->p_module );
609
610     return 0;
611 }
612
613 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
614 {
615     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
616         *pi_preroll = INT64_MAX;
617     else if( p->i_pts > 0 )
618         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
619     else if( p->i_dts > 0 )
620         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
621 }
622 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
623 {
624     input_thread_t *p_input = p_dec->p_owner->p_input;
625     const int i_rate = p_block->i_rate;
626     aout_buffer_t *p_aout_buf;
627
628     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
629     {
630         vlc_mutex_lock( &p_input->p->counters.counters_lock );
631         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio, 1, NULL );
632         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
633
634         if( p_aout_buf->start_date < p_dec->p_owner->i_preroll_end )
635         {
636             aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
637                                   p_dec->p_owner->p_aout_input, p_aout_buf );
638             continue;
639         }
640
641         if( p_dec->p_owner->i_preroll_end > 0 )
642         {
643             /* FIXME TODO flush audio output (don't know how to do that) */
644             msg_Dbg( p_dec, "End of audio preroll" );
645             p_dec->p_owner->i_preroll_end = -1;
646         }
647         aout_DecPlay( p_dec->p_owner->p_aout,
648                       p_dec->p_owner->p_aout_input,
649                       p_aout_buf, i_rate );
650     }
651 }
652 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
653 {
654     block_t *p_cc;
655     bool pb_present[4];
656     int i;
657     int i_cc_decoder;
658
659     assert( p_dec_cc->pf_get_cc != NULL );
660
661     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
662     if( !p_dec->p_owner->b_cc_supported )
663         return;
664
665     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
666     if( !p_cc )
667         return;
668
669     vlc_mutex_lock( &p_dec->p_owner->lock_cc );
670     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
671     {
672         p_dec->p_owner->pb_cc_present[i] |= pb_present[i];
673         if( p_dec->p_owner->pp_cc[i] )
674             i_cc_decoder++;
675     }
676
677     for( i = 0; i < 4; i++ )
678     {
679         if( !p_dec->p_owner->pp_cc[i] )
680             continue;
681
682         if( i_cc_decoder > 1 )
683             DecoderDecode( p_dec->p_owner->pp_cc[i], block_Duplicate( p_cc ) );
684         else
685             DecoderDecode( p_dec->p_owner->pp_cc[i], p_cc );
686         i_cc_decoder--;
687     }
688     vlc_mutex_unlock( &p_dec->p_owner->lock_cc );
689 }
690 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
691 {
692     vlc_mutex_lock( &p_vout->picture_lock );
693
694     if( p_pic->i_status == READY_PICTURE )
695     {
696         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
697         p_pic->date = 1;
698     }
699     else if( p_pic->i_refcount > 0 )
700     {
701         p_pic->i_status = DISPLAYED_PICTURE;
702     }
703     else
704     {
705         p_pic->i_status = DESTROYED_PICTURE;
706         p_vout->i_heap_size--;
707     }
708
709     vlc_mutex_unlock( &p_vout->picture_lock );
710 }
711 static void VoutFlushPicture( vout_thread_t *p_vout )
712 {
713     int i;
714     vlc_mutex_lock( &p_vout->picture_lock );
715     for( i = 0; i < p_vout->render.i_pictures; i++ )
716     {
717         picture_t *p_pic = p_vout->render.pp_picture[i];
718
719         if( p_pic->i_status == READY_PICTURE ||
720             p_pic->i_status == DISPLAYED_PICTURE )
721         {
722             /* We cannot change picture status if it is in READY_PICTURE state,
723              * Just make sure they won't be displayed */
724             p_pic->date = 1;
725         }
726     }
727     vlc_mutex_unlock( &p_vout->picture_lock );
728 }
729 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
730 {
731     input_thread_t *p_input = p_dec->p_owner->p_input;
732     picture_t *p_pic;
733
734     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
735     {
736         vlc_mutex_lock( &p_input->p->counters.counters_lock );
737         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video, 1, NULL );
738         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
739
740         if( p_pic->date < p_dec->p_owner->i_preroll_end )
741         {
742             VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
743             continue;
744         }
745
746         if( p_dec->p_owner->i_preroll_end > 0 )
747         {
748             msg_Dbg( p_dec, "End of video preroll" );
749             if( p_dec->p_owner->p_vout )
750                 VoutFlushPicture( p_dec->p_owner->p_vout );
751             /* */
752             p_dec->p_owner->i_preroll_end = -1;
753         }
754
755         if( ( !p_dec->p_owner->p_packetizer || !p_dec->p_owner->p_packetizer->pf_get_cc ) && p_dec->pf_get_cc )
756             DecoderGetCc( p_dec, p_dec );
757
758         vout_DatePicture( p_dec->p_owner->p_vout, p_pic,
759                           p_pic->date );
760         vout_DisplayPicture( p_dec->p_owner->p_vout, p_pic );
761     }
762 }
763
764 /**
765  * Decode a block
766  *
767  * \param p_dec the decoder object
768  * \param p_block the block to decode
769  * \return VLC_SUCCESS or an error code
770  */
771 static int DecoderDecode( decoder_t *p_dec, block_t *p_block )
772 {
773     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
774     const int i_rate = p_block ? p_block->i_rate : INPUT_RATE_DEFAULT;
775
776     if( p_block && p_block->i_buffer <= 0 )
777     {
778         block_Release( p_block );
779         return VLC_SUCCESS;
780     }
781
782 #ifdef ENABLE_SOUT
783     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
784     {
785         block_t *p_sout_block;
786
787         while( ( p_sout_block =
788                      p_dec->pf_packetize( p_dec, p_block ? &p_block : 0 ) ) )
789         {
790             if( !p_dec->p_owner->p_sout_input )
791             {
792                 es_format_Copy( &p_dec->p_owner->sout, &p_dec->fmt_out );
793
794                 p_dec->p_owner->sout.i_group = p_dec->fmt_in.i_group;
795                 p_dec->p_owner->sout.i_id = p_dec->fmt_in.i_id;
796                 if( p_dec->fmt_in.psz_language )
797                 {
798                     if( p_dec->p_owner->sout.psz_language )
799                         free( p_dec->p_owner->sout.psz_language );
800                     p_dec->p_owner->sout.psz_language =
801                         strdup( p_dec->fmt_in.psz_language );
802                 }
803
804                 p_dec->p_owner->p_sout_input =
805                     sout_InputNew( p_dec->p_owner->p_sout,
806                                    &p_dec->p_owner->sout );
807
808                 if( p_dec->p_owner->p_sout_input == NULL )
809                 {
810                     msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
811                              (char *)&p_dec->p_owner->sout.i_codec );
812                     p_dec->b_error = true;
813
814                     while( p_sout_block )
815                     {
816                         block_t *p_next = p_sout_block->p_next;
817                         block_Release( p_sout_block );
818                         p_sout_block = p_next;
819                     }
820                     break;
821                 }
822             }
823
824             while( p_sout_block )
825             {
826                 block_t *p_next = p_sout_block->p_next;
827
828                 p_sout_block->p_next = NULL;
829                 p_sout_block->i_rate = i_rate;
830
831                 sout_InputSendBuffer( p_dec->p_owner->p_sout_input,
832                                       p_sout_block );
833
834                 p_sout_block = p_next;
835             }
836
837             /* For now it's enough, as only sout impact on this flag */
838             if( p_dec->p_owner->p_sout->i_out_pace_nocontrol > 0 &&
839                 p_dec->p_owner->p_input->p->b_out_pace_control )
840             {
841                 msg_Dbg( p_dec, "switching to sync mode" );
842                 p_dec->p_owner->p_input->p->b_out_pace_control = false;
843             }
844             else if( p_dec->p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
845                      !p_dec->p_owner->p_input->p->b_out_pace_control )
846             {
847                 msg_Dbg( p_dec, "switching to async mode" );
848                 p_dec->p_owner->p_input->p->b_out_pace_control = true;
849             }
850         }
851     }
852     else
853 #endif
854     if( p_dec->fmt_in.i_cat == AUDIO_ES )
855     {
856         if( p_block )
857             DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
858
859         if( p_dec->p_owner->p_packetizer )
860         {
861             block_t *p_packetized_block;
862             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
863
864             while( (p_packetized_block =
865                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
866             {
867                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
868                 {
869                     es_format_Clean( &p_dec->fmt_in );
870                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
871                 }
872
873                 while( p_packetized_block )
874                 {
875                     block_t *p_next = p_packetized_block->p_next;
876                     p_packetized_block->p_next = NULL;
877                     p_packetized_block->i_rate = i_rate;
878
879                     DecoderDecodeAudio( p_dec, p_packetized_block );
880
881                     p_packetized_block = p_next;
882                 }
883             }
884         }
885         else if( p_block )
886         {
887             DecoderDecodeAudio( p_dec, p_block );
888         }
889     }
890     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
891     {
892         if( p_block )
893             DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
894
895         if( p_dec->p_owner->p_packetizer )
896         {
897             block_t *p_packetized_block;
898             decoder_t *p_packetizer = p_dec->p_owner->p_packetizer;
899
900             while( (p_packetized_block =
901                     p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
902             {
903                 if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
904                 {
905                     es_format_Clean( &p_dec->fmt_in );
906                     es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
907                 }
908                 if( p_packetizer->pf_get_cc )
909                     DecoderGetCc( p_dec, p_packetizer );
910
911                 while( p_packetized_block )
912                 {
913                     block_t *p_next = p_packetized_block->p_next;
914                     p_packetized_block->p_next = NULL;
915                     p_packetized_block->i_rate = i_rate;
916
917                     DecoderDecodeVideo( p_dec, p_packetized_block );
918
919                     p_packetized_block = p_next;
920                 }
921             }
922         }
923         else if( p_block )
924         {
925             DecoderDecodeVideo( p_dec, p_block );
926         }
927     }
928     else if( p_dec->fmt_in.i_cat == SPU_ES )
929     {
930         input_thread_t *p_input = p_dec->p_owner->p_input;
931         vout_thread_t *p_vout;
932         subpicture_t *p_spu;
933
934         if( p_block )
935             DecoderUpdatePreroll( &p_dec->p_owner->i_preroll_end, p_block );
936
937         while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
938         {
939             vlc_mutex_lock( &p_input->p->counters.counters_lock );
940             stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
941             vlc_mutex_unlock( &p_input->p->counters.counters_lock );
942
943             p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
944             if( p_vout && p_sys->p_spu_vout == p_vout )
945             {
946                 /* Prerool does not work very well with subtitle */
947                 if( p_spu->i_start < p_dec->p_owner->i_preroll_end &&
948                     ( p_spu->i_stop <= 0 || p_spu->i_stop < p_dec->p_owner->i_preroll_end ) )
949                     spu_DestroySubpicture( p_vout->p_spu, p_spu );
950                 else
951                     spu_DisplaySubpicture( p_vout->p_spu, p_spu );
952             }
953             else
954             {
955                 msg_Warn( p_dec, "no vout found, leaking subpicture" );
956             }
957             if( p_vout )
958                 vlc_object_release( p_vout );
959         }
960     }
961     else
962     {
963         msg_Err( p_dec, "unknown ES format" );
964         p_dec->b_error = 1;
965     }
966
967     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
968 }
969
970 /**
971  * Destroys a decoder object
972  *
973  * \param p_dec the decoder object
974  * \return nothing
975  */
976 static void DeleteDecoder( decoder_t * p_dec )
977 {
978     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
979              (char*)&p_dec->fmt_in.i_codec,
980              (unsigned)block_FifoCount( p_dec->p_owner->p_fifo ) );
981
982     /* Free all packets still in the decoder fifo. */
983     block_FifoEmpty( p_dec->p_owner->p_fifo );
984     block_FifoRelease( p_dec->p_owner->p_fifo );
985
986     /* Cleanup */
987     if( p_dec->p_owner->p_aout_input )
988         aout_DecDelete( p_dec->p_owner->p_aout, p_dec->p_owner->p_aout_input );
989     if( p_dec->p_owner->p_aout )
990     {
991         vlc_object_release( p_dec->p_owner->p_aout );
992         p_dec->p_owner->p_aout = NULL;
993     }
994     if( p_dec->p_owner->p_vout )
995     {
996         int i_pic;
997
998 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
999         /* Hack to make sure all the the pictures are freed by the decoder */
1000         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1001              i_pic++ )
1002         {
1003             if( p_pic->i_status == RESERVED_PICTURE )
1004                 vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1005             if( p_pic->i_refcount > 0 )
1006                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1007         }
1008 #undef p_pic
1009
1010         /* We are about to die. Reattach video output to p_vlc. */
1011         vout_Request( p_dec, p_dec->p_owner->p_vout, 0 );
1012     }
1013
1014 #ifdef ENABLE_SOUT
1015     if( p_dec->p_owner->p_sout_input )
1016     {
1017         sout_InputDelete( p_dec->p_owner->p_sout_input );
1018         es_format_Clean( &p_dec->p_owner->sout );
1019     }
1020 #endif
1021
1022     if( p_dec->fmt_in.i_cat == SPU_ES )
1023     {
1024         vout_thread_t *p_vout;
1025
1026         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1027         if( p_vout )
1028         {
1029             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1030                          p_dec->p_owner->i_spu_channel );
1031             vlc_object_release( p_vout );
1032         }
1033     }
1034
1035     es_format_Clean( &p_dec->fmt_in );
1036     es_format_Clean( &p_dec->fmt_out );
1037
1038     if( p_dec->p_owner->p_packetizer )
1039     {
1040         module_Unneed( p_dec->p_owner->p_packetizer,
1041                        p_dec->p_owner->p_packetizer->p_module );
1042         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
1043         es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_out );
1044         vlc_object_detach( p_dec->p_owner->p_packetizer );
1045         vlc_object_release( p_dec->p_owner->p_packetizer );
1046     }
1047
1048     vlc_mutex_destroy( &p_dec->p_owner->lock_cc );
1049
1050     vlc_object_detach( p_dec );
1051
1052     free( p_dec->p_owner );
1053 }
1054
1055 /*****************************************************************************
1056  * Buffers allocation callbacks for the decoders
1057  *****************************************************************************/
1058 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1059 {
1060     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1061     aout_buffer_t *p_buffer;
1062
1063     if( p_sys->p_aout_input != NULL &&
1064         ( p_dec->fmt_out.audio.i_rate != p_sys->audio.i_rate ||
1065           p_dec->fmt_out.audio.i_original_channels !=
1066               p_sys->audio.i_original_channels ||
1067           p_dec->fmt_out.audio.i_bytes_per_frame !=
1068               p_sys->audio.i_bytes_per_frame ) )
1069     {
1070         /* Parameters changed, restart the aout */
1071         aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
1072         p_sys->p_aout_input = NULL;
1073     }
1074
1075     if( p_sys->p_aout_input == NULL )
1076     {
1077         audio_sample_format_t format;
1078         int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1079
1080         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1081         p_sys->audio = p_dec->fmt_out.audio;
1082
1083         memcpy( &format, &p_sys->audio, sizeof( audio_sample_format_t ) );
1084         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1085                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1086         {
1087             if ( i_force_dolby == 1 )
1088             {
1089                 format.i_original_channels = format.i_original_channels |
1090                                              AOUT_CHAN_DOLBYSTEREO;
1091             }
1092             else /* i_force_dolby == 2 */
1093             {
1094                 format.i_original_channels = format.i_original_channels &
1095                                              ~AOUT_CHAN_DOLBYSTEREO;
1096             }
1097         }
1098
1099         p_sys->p_aout_input =
1100             aout_DecNew( p_dec, &p_sys->p_aout, &format, &p_dec->fmt_out.audio_replay_gain );
1101         if( p_sys->p_aout_input == NULL )
1102         {
1103             msg_Err( p_dec, "failed to create audio output" );
1104             p_dec->b_error = true;
1105             return NULL;
1106         }
1107         p_dec->fmt_out.audio.i_bytes_per_frame =
1108             p_sys->audio.i_bytes_per_frame;
1109     }
1110
1111     p_buffer = aout_DecNewBuffer( p_sys->p_aout_input, i_samples );
1112
1113     return p_buffer;
1114 }
1115
1116 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1117 {
1118     aout_DecDeleteBuffer( p_dec->p_owner->p_aout,
1119                           p_dec->p_owner->p_aout_input, p_buffer );
1120 }
1121
1122 static picture_t *vout_new_buffer( decoder_t *p_dec )
1123 {
1124     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1125     picture_t *p_pic;
1126
1127     if( p_sys->p_vout == NULL ||
1128         p_dec->fmt_out.video.i_width != p_sys->video.i_width ||
1129         p_dec->fmt_out.video.i_height != p_sys->video.i_height ||
1130         p_dec->fmt_out.video.i_chroma != p_sys->video.i_chroma ||
1131         p_dec->fmt_out.video.i_aspect != p_sys->video.i_aspect )
1132     {
1133         if( !p_dec->fmt_out.video.i_width ||
1134             !p_dec->fmt_out.video.i_height )
1135         {
1136             /* Can't create a new vout without display size */
1137             return NULL;
1138         }
1139
1140         if( !p_dec->fmt_out.video.i_visible_width ||
1141             !p_dec->fmt_out.video.i_visible_height )
1142         {
1143             if( p_dec->fmt_in.video.i_visible_width &&
1144                 p_dec->fmt_in.video.i_visible_height )
1145             {
1146                 p_dec->fmt_out.video.i_visible_width =
1147                     p_dec->fmt_in.video.i_visible_width;
1148                 p_dec->fmt_out.video.i_visible_height =
1149                     p_dec->fmt_in.video.i_visible_height;
1150             }
1151             else
1152             {
1153                 p_dec->fmt_out.video.i_visible_width =
1154                     p_dec->fmt_out.video.i_width;
1155                 p_dec->fmt_out.video.i_visible_height =
1156                     p_dec->fmt_out.video.i_height;
1157             }
1158         }
1159
1160         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1161             var_CreateGetBool( p_dec, "hdtv-fix" ) )
1162         {
1163             p_dec->fmt_out.video.i_visible_height = 1080;
1164             p_dec->fmt_out.video.i_sar_num *= 135;
1165             p_dec->fmt_out.video.i_sar_den *= 136;
1166             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1167         }
1168
1169         if( !p_dec->fmt_out.video.i_sar_num ||
1170             !p_dec->fmt_out.video.i_sar_den )
1171         {
1172             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1173               p_dec->fmt_out.video.i_visible_height;
1174
1175             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1176               p_dec->fmt_out.video.i_visible_width;
1177         }
1178
1179         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1180                      &p_dec->fmt_out.video.i_sar_den,
1181                      p_dec->fmt_out.video.i_sar_num,
1182                      p_dec->fmt_out.video.i_sar_den, 50000 );
1183
1184         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1185         p_sys->video = p_dec->fmt_out.video;
1186
1187         p_sys->p_vout = vout_Request( p_dec, p_sys->p_vout,
1188                                       &p_dec->fmt_out.video );
1189         if( p_sys->p_vout == NULL )
1190         {
1191             msg_Err( p_dec, "failed to create video output" );
1192             p_dec->b_error = true;
1193             return NULL;
1194         }
1195
1196         if( p_sys->video.i_rmask )
1197             p_sys->p_vout->render.i_rmask = p_sys->video.i_rmask;
1198         if( p_sys->video.i_gmask )
1199             p_sys->p_vout->render.i_gmask = p_sys->video.i_gmask;
1200         if( p_sys->video.i_bmask )
1201             p_sys->p_vout->render.i_bmask = p_sys->video.i_bmask;
1202     }
1203
1204     /* Get a new picture */
1205     while( !(p_pic = vout_CreatePicture( p_sys->p_vout, 0, 0, 0 ) ) )
1206     {
1207         int i_pic, i_ready_pic = 0;
1208
1209         if( p_dec->b_die || p_dec->b_error )
1210         {
1211             return NULL;
1212         }
1213
1214 #define p_pic p_dec->p_owner->p_vout->render.pp_picture[i_pic]
1215         /* Check the decoder doesn't leak pictures */
1216         for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1217              i_pic++ )
1218         {
1219             if( p_pic->i_status == READY_PICTURE )
1220             {
1221                 if( i_ready_pic++ > 0 ) break;
1222                 else continue;
1223             }
1224
1225             if( p_pic->i_status != DISPLAYED_PICTURE &&
1226                 p_pic->i_status != RESERVED_PICTURE &&
1227                 p_pic->i_status != READY_PICTURE ) break;
1228
1229             if( !p_pic->i_refcount && p_pic->i_status != RESERVED_PICTURE )
1230                 break;
1231         }
1232         if( i_pic == p_dec->p_owner->p_vout->render.i_pictures )
1233         {
1234             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1235
1236             /* Just free all the pictures */
1237             for( i_pic = 0; i_pic < p_dec->p_owner->p_vout->render.i_pictures;
1238                  i_pic++ )
1239             {
1240                 if( p_pic->i_status == RESERVED_PICTURE )
1241                     vout_DestroyPicture( p_dec->p_owner->p_vout, p_pic );
1242                 if( p_pic->i_refcount > 0 )
1243                 vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1244             }
1245         }
1246 #undef p_pic
1247
1248         msleep( VOUT_OUTMEM_SLEEP );
1249     }
1250
1251     return p_pic;
1252 }
1253
1254 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1255 {
1256     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1257 }
1258
1259 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1260 {
1261     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1262 }
1263
1264 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1265 {
1266     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1267 }
1268
1269 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1270 {
1271     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1272     vout_thread_t *p_vout = NULL;
1273     subpicture_t *p_subpic;
1274     int i_attempts = 30;
1275
1276     while( i_attempts-- )
1277     {
1278         if( p_dec->b_die || p_dec->b_error ) break;
1279
1280         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1281         if( p_vout ) break;
1282
1283         msleep( VOUT_DISPLAY_DELAY );
1284     }
1285
1286     if( !p_vout )
1287     {
1288         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1289         return NULL;
1290     }
1291
1292     if( p_sys->p_spu_vout != p_vout )
1293     {
1294         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1295                      &p_sys->i_spu_channel );
1296         p_sys->p_spu_vout = p_vout;
1297     }
1298
1299     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
1300     if( p_subpic )
1301     {
1302         p_subpic->i_channel = p_sys->i_spu_channel;
1303     }
1304
1305     vlc_object_release( p_vout );
1306
1307     return p_subpic;
1308 }
1309
1310 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1311 {
1312     decoder_owner_sys_t *p_sys = (decoder_owner_sys_t *)p_dec->p_owner;
1313     vout_thread_t *p_vout = NULL;
1314
1315     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1316     if( !p_vout || p_sys->p_spu_vout != p_vout )
1317     {
1318         if( p_vout )
1319             vlc_object_release( p_vout );
1320         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1321         return;
1322     }
1323
1324     spu_DestroySubpicture( p_vout->p_spu, p_subpic );
1325
1326     vlc_object_release( p_vout );
1327 }
1328