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