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