]> git.sesse.net Git - vlc/blob - src/input/decoder.c
e1e132f7d2490e1f50a397322074438220cb0a27
[vlc] / src / input / decoder.c
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
47 #include "input_clock.h"
48 #include "input_decoder.h"
49
50 #include "../video_output/vout_internal.h"
51
52 static decoder_t *CreateDecoder( input_thread_t *, es_format_t *, int, sout_instance_t *p_sout );
53 static void       DeleteDecoder( decoder_t * );
54
55 static void      *DecoderThread( vlc_object_t * );
56 static int        DecoderProcess( decoder_t * p_dec, block_t *p_block );
57 static void       DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date );
58
59 /* Buffers allocation callbacks for the decoders */
60 static aout_buffer_t *aout_new_buffer( decoder_t *, int );
61 static void aout_del_buffer( decoder_t *, aout_buffer_t * );
62
63 static picture_t *vout_new_buffer( decoder_t * );
64 static void vout_del_buffer( decoder_t *, picture_t * );
65 static void vout_link_picture( decoder_t *, picture_t * );
66 static void vout_unlink_picture( decoder_t *, picture_t * );
67
68 static subpicture_t *spu_new_buffer( decoder_t * );
69 static void spu_del_buffer( decoder_t *, subpicture_t * );
70
71 static es_format_t null_es_format;
72
73 struct decoder_owner_sys_t
74 {
75     bool      b_own_thread;
76
77     int64_t         i_preroll_end;
78
79     input_thread_t  *p_input;
80     input_clock_t   *p_clock;
81     int             i_last_rate;
82
83     vout_thread_t   *p_spu_vout;
84     int              i_spu_channel;
85     int64_t          i_spu_order;
86
87     sout_instance_t         *p_sout;
88     sout_packetizer_input_t *p_sout_input;
89
90     /* Some decoders require already packetized data (ie. not truncated) */
91     decoder_t *p_packetizer;
92
93     /* Current format in use by the output */
94     video_format_t video;
95     audio_format_t audio;
96     es_format_t    sout;
97
98     /* fifo */
99     block_fifo_t *p_fifo;
100
101     /* Lock for communication with decoder thread */
102     vlc_mutex_t lock;
103     vlc_cond_t  wait;
104
105     /* -- These variables need locking on write(only) -- */
106     aout_instance_t *p_aout;
107     aout_input_t    *p_aout_input;
108
109     vout_thread_t   *p_vout;
110
111     /* -- Theses variables need locking on read *and* write -- */
112     /* */
113     /* Pause */
114     bool b_paused;
115     mtime_t i_pause_date;
116
117     /* CC */
118     struct
119     {
120         bool b_supported;
121         bool pb_present[4];
122         decoder_t *pp_decoder[4];
123     } cc;
124
125     /* Delay */
126     mtime_t i_ts_delay;
127 };
128
129 /* */
130 static void DecoderUnsupportedCodec( decoder_t *p_dec, vlc_fourcc_t codec )
131 {
132     msg_Err( p_dec, "no suitable decoder module for fourcc `%4.4s'.\n"
133              "VLC probably does not support this sound or video format.",
134              (char*)&codec );
135     intf_UserFatal( p_dec, false, _("No suitable decoder module"), 
136                     _("VLC does not support the audio or video format \"%4.4s\". "
137                       "Unfortunately there is no way for you to fix this."), (char*)&codec );
138 }
139
140 /* decoder_GetInputAttachment:
141  */
142 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
143                                                 const char *psz_name )
144 {
145     input_attachment_t *p_attachment;
146     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
147         return NULL;
148     return p_attachment;
149 }
150 /* decoder_GetInputAttachments:
151  */
152 int decoder_GetInputAttachments( decoder_t *p_dec,
153                                  input_attachment_t ***ppp_attachment,
154                                  int *pi_attachment )
155 {
156     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
157                           ppp_attachment, pi_attachment );
158 }
159 /* decoder_GetDisplayDate:
160  */
161 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
162 {
163     decoder_owner_sys_t *p_owner = p_dec->p_owner;
164
165     if( !p_owner->p_clock )
166         return i_ts;
167     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
168 }
169 /* decoder_GetDisplayRate:
170  */
171 int decoder_GetDisplayRate( decoder_t *p_dec )
172 {
173     decoder_owner_sys_t *p_owner = p_dec->p_owner;
174
175     if( !p_owner->p_clock )
176         return INPUT_RATE_DEFAULT;
177     return input_clock_GetRate( p_owner->p_clock );
178 }
179
180 /**
181  * Spawns a new decoder thread
182  *
183  * \param p_input the input thread
184  * \param p_es the es descriptor
185  * \return the spawned decoder object
186  */
187 decoder_t *input_DecoderNew( input_thread_t *p_input,
188                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
189 {
190     decoder_t   *p_dec = NULL;
191     vlc_value_t val;
192
193 #ifndef ENABLE_SOUT
194     (void)b_force_decoder;
195 #else
196     /* If we are in sout mode, search for packetizer module */
197     if( p_sout )
198     {
199         /* Create the decoder configuration structure */
200         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_PACKETIZER, p_sout );
201         if( p_dec == NULL )
202         {
203             msg_Err( p_input, "could not create packetizer" );
204             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
205                             _("VLC could not open the packetizer module.") );
206             return NULL;
207         }
208     }
209     else
210 #endif
211     {
212         /* Create the decoder configuration structure */
213         p_dec = CreateDecoder( p_input, fmt, VLC_OBJECT_DECODER, p_sout );
214         if( p_dec == NULL )
215         {
216             msg_Err( p_input, "could not create decoder" );
217             intf_UserFatal( p_input, false, _("Streaming / Transcoding failed"),
218                             _("VLC could not open the decoder module.") );
219             return NULL;
220         }
221     }
222
223     if( !p_dec->p_module )
224     {
225         DecoderUnsupportedCodec( p_dec, fmt->i_codec );
226
227         DeleteDecoder( p_dec );
228         vlc_object_release( p_dec );
229         return NULL;
230     }
231
232     p_dec->p_owner->p_clock = p_clock;
233
234     if( p_sout && p_sout == p_input->p->p_sout && p_input->p->input.b_can_pace_control )
235     {
236         msg_Dbg( p_input, "stream out mode -> no decoder thread" );
237         p_dec->p_owner->b_own_thread = false;
238     }
239     else
240     {
241         var_Get( p_input, "minimize-threads", &val );
242         p_dec->p_owner->b_own_thread = !val.b_bool;
243     }
244
245     if( p_dec->p_owner->b_own_thread )
246     {
247         int i_priority;
248         if( fmt->i_cat == AUDIO_ES )
249             i_priority = VLC_THREAD_PRIORITY_AUDIO;
250         else
251             i_priority = VLC_THREAD_PRIORITY_VIDEO;
252
253         /* Spawn the decoder thread */
254         if( vlc_thread_create( p_dec, "decoder", DecoderThread,
255                                i_priority, false ) )
256         {
257             msg_Err( p_dec, "cannot spawn decoder thread" );
258             module_unneed( p_dec, p_dec->p_module );
259             DeleteDecoder( p_dec );
260             vlc_object_release( p_dec );
261             return NULL;
262         }
263     }
264
265     return p_dec;
266 }
267
268 /**
269  * Kills a decoder thread and waits until it's finished
270  *
271  * \param p_input the input thread
272  * \param p_es the es descriptor
273  * \return nothing
274  */
275 void input_DecoderDelete( decoder_t *p_dec )
276 {
277     decoder_owner_sys_t *p_owner = p_dec->p_owner;
278
279     vlc_object_kill( p_dec );
280
281     if( p_owner->b_own_thread )
282     {
283         /* Make sure we aren't paused anymore */
284         vlc_mutex_lock( &p_owner->lock );
285         if( p_owner->b_paused )
286         {
287             p_owner->b_paused = false;
288             vlc_cond_signal( &p_owner->wait );
289         }
290         vlc_mutex_unlock( &p_owner->lock );
291
292         /* Make sure the thread leaves the function by
293          * sending it an empty block. */
294         block_t *p_block = block_New( p_dec, 0 );
295         input_DecoderDecode( p_dec, p_block );
296
297         vlc_thread_join( p_dec );
298
299         /* Don't module_unneed() here because of the dll loader that wants
300          * close() in the same thread than open()/decode() */
301     }
302     else
303     {
304         /* Flush */
305         input_DecoderDecode( p_dec, NULL );
306
307         module_unneed( p_dec, p_dec->p_module );
308     }
309
310     /* */
311     if( p_dec->p_owner->cc.b_supported )
312     {
313         int i;
314         for( i = 0; i < 4; i++ )
315             input_DecoderSetCcState( p_dec, false, i );
316     }
317
318     /* Delete decoder configuration */
319     DeleteDecoder( p_dec );
320
321     /* Delete the decoder */
322     vlc_object_release( p_dec );
323 }
324
325 /**
326  * Put a block_t in the decoder's fifo.
327  *
328  * \param p_dec the decoder object
329  * \param p_block the data block
330  */
331 void input_DecoderDecode( decoder_t * p_dec, block_t *p_block )
332 {
333     decoder_owner_sys_t *p_owner = p_dec->p_owner;
334
335     if( p_owner->b_own_thread )
336     {
337         if( p_owner->p_input->p->b_out_pace_control )
338         {
339             /* FIXME !!!!! */
340             while( !p_dec->b_die && !p_dec->b_error &&
341                    block_FifoCount( p_owner->p_fifo ) > 10 )
342             {
343                 msleep( 1000 );
344             }
345         }
346         else if( block_FifoSize( p_owner->p_fifo ) > 50000000 /* 50 MB */ )
347         {
348             /* FIXME: ideally we would check the time amount of data
349              * in the fifo instead of its size. */
350             msg_Warn( p_dec, "decoder/packetizer fifo full (data not "
351                       "consumed quickly enough), resetting fifo!" );
352             block_FifoEmpty( p_owner->p_fifo );
353         }
354
355         block_FifoPut( p_owner->p_fifo, p_block );
356     }
357     else
358     {
359         if( p_dec->b_error || ( p_block && p_block->i_buffer <= 0 ) )
360         {
361             if( p_block )
362                 block_Release( p_block );
363         }
364         else
365         {
366             DecoderProcess( p_dec, p_block );
367         }
368     }
369 }
370
371 bool input_DecoderIsEmpty( decoder_t * p_dec )
372 {
373     if( p_dec->p_owner->b_own_thread &&
374         block_FifoCount( p_dec->p_owner->p_fifo ) > 0 )
375     {
376         return false;
377     }
378     return true;
379 }
380
381 void input_DecoderIsCcPresent( decoder_t *p_dec, bool pb_present[4] )
382 {
383     decoder_owner_sys_t *p_owner = p_dec->p_owner;
384     int i;
385
386     vlc_mutex_lock( &p_owner->lock );
387     for( i = 0; i < 4; i++ )
388         pb_present[i] =  p_owner->cc.pb_present[i];
389     vlc_mutex_unlock( &p_owner->lock );
390 }
391 int input_DecoderSetCcState( decoder_t *p_dec, bool b_decode, int i_channel )
392 {
393     decoder_owner_sys_t *p_owner = p_dec->p_owner;
394
395     //msg_Warn( p_dec, "input_DecoderSetCcState: %d @%d", b_decode, i_channel );
396
397     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
398         return VLC_EGENERIC;
399
400     if( b_decode )
401     {
402         static const vlc_fourcc_t fcc[4] = {
403             VLC_FOURCC('c', 'c', '1', ' '),
404             VLC_FOURCC('c', 'c', '2', ' '),
405             VLC_FOURCC('c', 'c', '3', ' '),
406             VLC_FOURCC('c', 'c', '4', ' '),
407         };
408         decoder_t *p_cc;
409         es_format_t fmt;
410
411         es_format_Init( &fmt, SPU_ES, fcc[i_channel] );
412         p_cc = CreateDecoder( p_owner->p_input, &fmt, VLC_OBJECT_DECODER, p_owner->p_sout );
413         if( !p_cc )
414         {
415             msg_Err( p_dec, "could not create decoder" );
416             intf_UserFatal( p_dec, false, _("Streaming / Transcoding failed"),
417                             _("VLC could not open the decoder module.") );
418             return VLC_EGENERIC;
419         }
420         else if( !p_cc->p_module )
421         {
422             DecoderUnsupportedCodec( p_dec, fcc[i_channel] );
423             DeleteDecoder( p_cc );
424             vlc_object_release( p_cc );
425             return VLC_EGENERIC;
426         }
427         p_cc->p_owner->p_clock = p_owner->p_clock;
428
429         vlc_mutex_lock( &p_owner->lock );
430         p_owner->cc.pp_decoder[i_channel] = p_cc;
431         vlc_mutex_unlock( &p_owner->lock );
432     }
433     else
434     {
435         decoder_t *p_cc;
436
437         vlc_mutex_lock( &p_owner->lock );
438         p_cc = p_owner->cc.pp_decoder[i_channel];
439         p_owner->cc.pp_decoder[i_channel] = NULL;
440         vlc_mutex_unlock( &p_owner->lock );
441
442         if( p_cc )
443         {
444             vlc_object_kill( p_cc );
445             module_unneed( p_cc, p_cc->p_module );
446             DeleteDecoder( p_cc );
447             vlc_object_release( p_cc );
448         }
449     }
450     return VLC_SUCCESS;
451 }
452 int input_DecoderGetCcState( decoder_t *p_dec, bool *pb_decode, int i_channel )
453 {
454     decoder_owner_sys_t *p_owner = p_dec->p_owner;
455
456     *pb_decode = false;
457     if( i_channel < 0 || i_channel >= 4 || !p_owner->cc.pb_present[i_channel] )
458         return VLC_EGENERIC;
459
460     vlc_mutex_lock( &p_owner->lock );
461     *pb_decode = p_owner->cc.pp_decoder[i_channel] != NULL;
462     vlc_mutex_unlock( &p_owner->lock );
463     return VLC_EGENERIC;
464 }
465
466 void input_DecoderChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
467 {
468     decoder_owner_sys_t *p_owner = p_dec->p_owner;
469
470     vlc_mutex_lock( &p_owner->lock );
471
472     assert( !p_owner->b_paused || !b_paused );
473     p_owner->b_paused = b_paused;
474     p_owner->i_pause_date = i_date;
475     if( p_owner->b_own_thread )
476         vlc_cond_signal( &p_owner->wait );
477
478     DecoderOutputChangePause( p_dec, b_paused, i_date );
479     vlc_mutex_unlock( &p_owner->lock );
480 }
481
482 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
483 {
484     decoder_owner_sys_t *p_owner = p_dec->p_owner;
485
486     vlc_mutex_lock( &p_owner->lock );
487     p_owner->i_ts_delay = i_delay;
488     vlc_mutex_unlock( &p_owner->lock );
489 }
490
491 void input_DecoderFlush( decoder_t *p_dec )
492 {
493     decoder_owner_sys_t *p_owner = p_dec->p_owner;
494     block_t *p_null;
495
496     /* Empty the fifo */
497     if( p_owner->b_own_thread )
498         block_FifoEmpty( p_owner->p_fifo );
499
500     /* Send a special block */
501     p_null = block_New( p_dec, 128 );
502     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
503     p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
504     if( !p_dec->fmt_in.b_packetized )
505         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
506     memset( p_null->p_buffer, 0, p_null->i_buffer );
507
508     input_DecoderDecode( p_dec, p_null );
509 }
510
511 /**
512  * Create a decoder object
513  *
514  * \param p_input the input thread
515  * \param p_es the es descriptor
516  * \param i_object_type Object type as define in include/vlc_objects.h
517  * \return the decoder object
518  */
519 static decoder_t * CreateDecoder( input_thread_t *p_input,
520                                   es_format_t *fmt, int i_object_type, sout_instance_t *p_sout )
521 {
522     decoder_t *p_dec;
523     decoder_owner_sys_t *p_owner;
524     int i;
525
526     p_dec = vlc_object_create( p_input, i_object_type );
527     if( p_dec == NULL )
528         return NULL;
529
530     p_dec->pf_decode_audio = NULL;
531     p_dec->pf_decode_video = NULL;
532     p_dec->pf_decode_sub = NULL;
533     p_dec->pf_get_cc = NULL;
534     p_dec->pf_packetize = NULL;
535
536     /* Initialize the decoder fifo */
537     p_dec->p_module = NULL;
538
539     memset( &null_es_format, 0, sizeof(es_format_t) );
540     es_format_Copy( &p_dec->fmt_in, fmt );
541     es_format_Copy( &p_dec->fmt_out, &null_es_format );
542
543     /* Allocate our private structure for the decoder */
544     p_dec->p_owner = p_owner = malloc( sizeof( decoder_owner_sys_t ) );
545     if( p_dec->p_owner == NULL )
546     {
547         vlc_object_release( p_dec );
548         return NULL;
549     }
550     p_dec->p_owner->b_own_thread = true;
551     p_dec->p_owner->i_preroll_end = -1;
552     p_dec->p_owner->i_last_rate = INPUT_RATE_DEFAULT;
553     p_dec->p_owner->p_input = p_input;
554     p_dec->p_owner->p_aout = NULL;
555     p_dec->p_owner->p_aout_input = NULL;
556     p_dec->p_owner->p_vout = NULL;
557     p_dec->p_owner->p_spu_vout = NULL;
558     p_dec->p_owner->i_spu_channel = 0;
559     p_dec->p_owner->i_spu_order = 0;
560     p_dec->p_owner->p_sout = p_sout;
561     p_dec->p_owner->p_sout_input = NULL;
562     p_dec->p_owner->p_packetizer = NULL;
563
564     /* decoder fifo */
565     if( ( p_dec->p_owner->p_fifo = block_FifoNew() ) == NULL )
566     {
567         free( p_dec->p_owner );
568         vlc_object_release( p_dec );
569         return NULL;
570     }
571
572     /* Set buffers allocation callbacks for the decoders */
573     p_dec->pf_aout_buffer_new = aout_new_buffer;
574     p_dec->pf_aout_buffer_del = aout_del_buffer;
575     p_dec->pf_vout_buffer_new = vout_new_buffer;
576     p_dec->pf_vout_buffer_del = vout_del_buffer;
577     p_dec->pf_picture_link    = vout_link_picture;
578     p_dec->pf_picture_unlink  = vout_unlink_picture;
579     p_dec->pf_spu_buffer_new  = spu_new_buffer;
580     p_dec->pf_spu_buffer_del  = spu_del_buffer;
581
582     vlc_object_attach( p_dec, p_input );
583
584     /* Find a suitable decoder/packetizer module */
585     if( i_object_type == VLC_OBJECT_DECODER )
586         p_dec->p_module = module_need( p_dec, "decoder", "$codec", 0 );
587     else
588         p_dec->p_module = module_need( p_dec, "packetizer", "$packetizer", 0 );
589
590     /* Check if decoder requires already packetized data */
591     if( i_object_type == VLC_OBJECT_DECODER &&
592         p_dec->b_need_packetized && !p_dec->fmt_in.b_packetized )
593     {
594         p_dec->p_owner->p_packetizer =
595             vlc_object_create( p_input, VLC_OBJECT_PACKETIZER );
596         if( p_dec->p_owner->p_packetizer )
597         {
598             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_in,
599                             &p_dec->fmt_in );
600
601             es_format_Copy( &p_dec->p_owner->p_packetizer->fmt_out,
602                             &null_es_format );
603
604             vlc_object_attach( p_dec->p_owner->p_packetizer, p_input );
605
606             p_dec->p_owner->p_packetizer->p_module =
607                 module_need( p_dec->p_owner->p_packetizer,
608                              "packetizer", "$packetizer", 0 );
609
610             if( !p_dec->p_owner->p_packetizer->p_module )
611             {
612                 es_format_Clean( &p_dec->p_owner->p_packetizer->fmt_in );
613                 vlc_object_detach( p_dec->p_owner->p_packetizer );
614                 vlc_object_release( p_dec->p_owner->p_packetizer );
615             }
616         }
617     }
618
619     /* Copy ourself the input replay gain */
620     if( fmt->i_cat == AUDIO_ES )
621     {
622         for( i = 0; i < AUDIO_REPLAY_GAIN_MAX; i++ )
623         {
624             if( !p_dec->fmt_out.audio_replay_gain.pb_peak[i] )
625             {
626                 p_dec->fmt_out.audio_replay_gain.pb_peak[i] = fmt->audio_replay_gain.pb_peak[i];
627                 p_dec->fmt_out.audio_replay_gain.pf_peak[i] = fmt->audio_replay_gain.pf_peak[i];
628             }
629             if( !p_dec->fmt_out.audio_replay_gain.pb_gain[i] )
630             {
631                 p_dec->fmt_out.audio_replay_gain.pb_gain[i] = fmt->audio_replay_gain.pb_gain[i];
632                 p_dec->fmt_out.audio_replay_gain.pf_gain[i] = fmt->audio_replay_gain.pf_gain[i];
633             }
634         }
635     }
636     /* */
637     p_owner->cc.b_supported = false;
638     if( i_object_type == VLC_OBJECT_DECODER )
639     {
640         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
641             p_owner->cc.b_supported = true;
642         if( p_dec->pf_get_cc )
643             p_owner->cc.b_supported = true;
644     }
645
646     vlc_mutex_init( &p_owner->lock );
647     vlc_cond_init( &p_owner->wait );
648     p_owner->b_paused = false;
649     p_owner->i_pause_date = 0;
650     for( i = 0; i < 4; i++ )
651     {
652         p_owner->cc.pb_present[i] = false;
653         p_owner->cc.pp_decoder[i] = NULL;
654     }
655     p_owner->i_ts_delay = 0;
656     return p_dec;
657 }
658
659 /**
660  * The decoding main loop
661  *
662  * \param p_dec the decoder
663  */
664 static void *DecoderThread( vlc_object_t *p_this )
665 {
666     decoder_t *p_dec = (decoder_t *)p_this;
667     decoder_owner_sys_t *p_owner = p_dec->p_owner;
668
669     block_t *p_block;
670     int canc = vlc_savecancel();
671
672     /* The decoder's main loop */
673     while( vlc_object_alive( p_dec ) && !p_dec->b_error )
674     {
675         if( ( p_block = block_FifoGet( p_owner->p_fifo ) ) == NULL )
676         {
677             p_dec->b_error = true;
678             break;
679         }
680
681         if( DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
682             break;
683     }
684
685     while( vlc_object_alive( p_dec ) )
686     {
687         /* Trash all received PES packets */
688         p_block = block_FifoGet( p_owner->p_fifo );
689         if( p_block )
690             block_Release( p_block );
691     }
692
693     /* We do it here because of the dll loader that wants close() in the
694      * same thread than open()/decode() */
695     module_unneed( p_dec, p_dec->p_module );
696     vlc_restorecancel( canc );
697     return NULL;
698 }
699
700 static void DecoderWaitUnpause( decoder_t *p_dec )
701 {
702     decoder_owner_sys_t *p_owner = p_dec->p_owner;
703
704     vlc_mutex_lock( &p_owner->lock );
705
706     while( p_owner->b_paused )
707         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
708
709     vlc_mutex_unlock( &p_owner->lock );
710 }
711
712 static void DecoderGetDelays( decoder_t *p_dec, mtime_t *pi_ts_delay, mtime_t *pi_es_delay )
713 {
714     decoder_owner_sys_t *p_owner = p_dec->p_owner;
715
716     *pi_ts_delay = p_owner->p_input->i_pts_delay;
717
718     vlc_mutex_lock( &p_owner->lock );
719
720     *pi_es_delay = p_owner->i_ts_delay;
721
722     vlc_mutex_unlock( &p_owner->lock );
723 }
724
725 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
726 {
727     decoder_owner_sys_t *p_owner = p_dec->p_owner;
728
729     vlc_assert_locked( &p_owner->lock );
730
731     /* XXX only audio and video output have to be paused.
732      * - for sout it is useless
733      * - for subs, it is done by the vout
734      */
735     if( p_dec->fmt_in.i_cat == AUDIO_ES )
736     {
737         if( p_owner->p_aout && p_owner->p_aout_input )
738             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
739                                  b_paused, i_date );
740     }
741     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
742     {
743         if( p_owner->p_vout )
744             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
745     }
746 }
747 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
748 {
749     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
750         *pi_preroll = INT64_MAX;
751     else if( p->i_pts > 0 )
752         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
753     else if( p->i_dts > 0 )
754         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
755 }
756
757 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
758 {
759     mtime_t current_date = mdate();
760
761     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
762     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
763     {
764         /* ETSI EN 300 472 Annex A : do not take into account the PTS
765          * for teletext streams. */
766         return current_date + 400000 + i_ts_delay;
767     }
768     return i_ts;
769 }
770
771 static void DecoderSoutBufferFixTs( block_t *p_block,
772                                     input_clock_t *p_clock,
773                                     mtime_t i_ts_delay, mtime_t i_es_delay,
774                                     bool b_teletext )
775 {
776     assert( p_clock );
777
778     if( !p_block->i_dts && !p_block->i_pts )
779         p_block->i_rate = input_clock_GetRate( p_clock );
780
781     if( p_block->i_dts > 0 )
782         p_block->i_dts = input_clock_GetTS( p_clock, &p_block->i_rate,
783                                             i_ts_delay, p_block->i_dts + i_es_delay);
784
785     if( p_block->i_pts > 0 )
786         p_block->i_pts = input_clock_GetTS( p_clock, &p_block->i_rate,
787                                             i_ts_delay, p_block->i_pts + i_es_delay );
788
789     if( p_block->i_length > 0 )
790         p_block->i_length = ( p_block->i_length * p_block->i_rate +
791                                 INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
792
793     if( b_teletext )
794         p_block->i_pts = DecoderTeletextFixTs( p_block->i_pts, i_ts_delay );
795 }
796 static void DecoderAoutBufferFixTs( aout_buffer_t *p_buffer, int *pi_rate,
797                                     input_clock_t *p_clock,
798                                     mtime_t i_ts_delay, mtime_t i_es_delay )
799 {
800     /* sout display module does not set clock */
801     if( !p_clock )
802         return;
803
804     if( !p_buffer->start_date && !p_buffer->end_date )
805         *pi_rate = input_clock_GetRate( p_clock );
806
807     if( p_buffer->start_date > 0 )
808         p_buffer->start_date =
809             input_clock_GetTS( p_clock, pi_rate,
810                                i_ts_delay, p_buffer->start_date + i_es_delay );
811
812     if( p_buffer->end_date > 0 )
813         p_buffer->end_date =
814             input_clock_GetTS( p_clock, pi_rate,
815                                i_ts_delay, p_buffer->end_date + i_es_delay );
816 }
817 static void DecoderVoutBufferFixTs( picture_t *p_picture, int *pi_rate,
818                                     input_clock_t *p_clock,
819                                     mtime_t i_ts_delay, mtime_t i_es_delay )
820 {
821     /* sout display module does not set clock */
822     if( !p_clock )
823         return;
824
825     if( p_picture->date > 0 )
826         p_picture->date =
827             input_clock_GetTS( p_clock, pi_rate,
828                                i_ts_delay, p_picture->date + i_es_delay );
829 }
830 static void DecoderSpuBufferFixTs( subpicture_t *p_subpic,
831                                    input_clock_t *p_clock,
832                                    mtime_t i_ts_delay, mtime_t i_es_delay,
833                                    bool b_teletext )
834 {
835     bool b_ephemere = p_subpic->i_start == p_subpic->i_stop;
836
837     /* sout display module does not set clock */
838     if( !p_clock )
839         return;
840
841     if( p_subpic->i_start > 0 )
842         p_subpic->i_start =
843             input_clock_GetTS( p_clock, NULL,
844                                i_ts_delay, p_subpic->i_start + i_es_delay );
845
846     if( p_subpic->i_stop > 0 )
847         p_subpic->i_stop =
848             input_clock_GetTS( p_clock, NULL,
849                                i_ts_delay, p_subpic->i_stop + i_es_delay );
850
851     /* Do not create ephemere picture because of rounding errors */
852     if( !b_ephemere && p_subpic->i_start == p_subpic->i_stop )
853         p_subpic->i_stop++;
854
855     if( b_teletext )
856         p_subpic->i_start = DecoderTeletextFixTs( p_subpic->i_start, i_ts_delay );
857 }
858
859 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
860 {
861     decoder_owner_sys_t *p_owner = p_dec->p_owner;
862     input_thread_t  *p_input = p_owner->p_input;
863     input_clock_t   *p_clock = p_owner->p_clock;
864     aout_buffer_t   *p_aout_buf;
865     int i_decoded = 0;
866     int i_lost = 0;
867     int i_played = 0;
868
869     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
870     {
871         aout_instance_t *p_aout = p_owner->p_aout;
872         aout_input_t    *p_aout_input = p_owner->p_aout_input;
873         int i_lost = 0;
874         int i_played;
875
876         if( p_dec->b_die )
877         {
878             /* It prevent freezing VLC in case of broken decoder */
879             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
880             if( p_block )
881                 block_Release( p_block );
882             break;
883         }
884         i_decoded++;
885
886         if( p_aout_buf->start_date < p_owner->i_preroll_end )
887         {
888             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
889             continue;
890         }
891
892         if( p_owner->i_preroll_end > 0 )
893         {
894             /* FIXME TODO flush audio output (don't know how to do that) */
895             msg_Dbg( p_dec, "End of audio preroll" );
896             p_owner->i_preroll_end = -1;
897         }
898
899         DecoderWaitUnpause( p_dec );
900
901         int i_rate = INPUT_RATE_DEFAULT;
902         mtime_t i_ts_delay;
903         mtime_t i_es_delay;
904         DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
905
906         DecoderAoutBufferFixTs( p_aout_buf, &i_rate, p_clock, i_ts_delay, i_es_delay );
907
908         if( !p_clock && p_block && p_block->i_rate > 0 )
909             i_rate = p_block->i_rate;
910
911         /* FIXME TODO take care of audio-delay for mdate check */
912         const mtime_t i_max_date = mdate() + i_ts_delay +
913             i_es_delay * i_rate / INPUT_RATE_DEFAULT + AOUT_MAX_ADVANCE_TIME;
914
915         if( p_aout_buf->start_date > 0 &&
916             p_aout_buf->start_date <= i_max_date &&
917             i_rate >= INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE &&
918             i_rate <= INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
919         {
920             /* Wait if we are too early
921              * FIXME that's plain ugly to do it here */
922             mwait( p_aout_buf->start_date - AOUT_MAX_PREPARE_TIME );
923
924             if( !aout_DecPlay( p_aout, p_aout_input, p_aout_buf, i_rate ) )
925                 i_played++;
926             i_lost += aout_DecGetResetLost( p_aout, p_aout_input );
927         }
928         else
929         {
930             if( p_aout_buf->start_date <= 0 )
931             {
932                 msg_Warn( p_dec, "non-dated audio buffer received" );
933             }
934             else if( p_aout_buf->start_date > i_max_date )
935             {
936                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
937                           p_aout_buf->start_date - mdate() );
938             }
939             i_lost++;
940             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
941         }
942
943     }
944
945     /* Update ugly stat */
946     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
947     {
948         vlc_mutex_lock( &p_input->p->counters.counters_lock);
949
950         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
951                              i_lost, NULL );
952         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
953                              i_played, NULL );
954         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
955                              i_decoded, NULL );
956
957         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
958     }
959 }
960 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
961 {
962     decoder_owner_sys_t *p_owner = p_dec->p_owner;
963     block_t *p_cc;
964     bool pb_present[4];
965     int i;
966     int i_cc_decoder;
967
968     assert( p_dec_cc->pf_get_cc != NULL );
969
970     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
971     if( !p_owner->cc.b_supported )
972         return;
973
974     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
975     if( !p_cc )
976         return;
977
978     vlc_mutex_lock( &p_owner->lock );
979     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
980     {
981         p_owner->cc.pb_present[i] |= pb_present[i];
982         if( p_owner->cc.pp_decoder[i] )
983             i_cc_decoder++;
984     }
985
986     for( i = 0; i < 4; i++ )
987     {
988         if( !p_owner->cc.pp_decoder[i] )
989             continue;
990
991         if( i_cc_decoder > 1 )
992             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
993         else
994             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
995         i_cc_decoder--;
996     }
997     vlc_mutex_unlock( &p_owner->lock );
998 }
999 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
1000 {
1001     vlc_mutex_lock( &p_vout->picture_lock );
1002
1003     if( p_pic->i_status == READY_PICTURE )
1004     {
1005         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
1006         p_pic->date = 1;
1007     }
1008     else if( p_pic->i_refcount > 0 )
1009     {
1010         p_pic->i_status = DISPLAYED_PICTURE;
1011     }
1012     else
1013     {
1014         p_pic->i_status = DESTROYED_PICTURE;
1015         picture_CleanupQuant( p_pic );
1016         p_vout->i_heap_size--;
1017     }
1018
1019     vlc_mutex_unlock( &p_vout->picture_lock );
1020 }
1021 static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
1022 {
1023     int i;
1024     vlc_mutex_lock( &p_vout->picture_lock );
1025     for( i = 0; i < p_vout->render.i_pictures; i++ )
1026     {
1027         picture_t *p_pic = p_vout->render.pp_picture[i];
1028
1029         if( p_pic->i_status == READY_PICTURE ||
1030             p_pic->i_status == DISPLAYED_PICTURE )
1031         {
1032             /* We cannot change picture status if it is in READY_PICTURE state,
1033              * Just make sure they won't be displayed */
1034             if( p_pic->date > i_max_date )
1035                 p_pic->date = i_max_date;
1036         }
1037     }
1038     vlc_mutex_unlock( &p_vout->picture_lock );
1039 }
1040
1041 #if 0
1042 static void DecoderOptimizePtsDelay( decoder_t *p_dec )
1043 {
1044     input_thread_t *p_input = p_dec->p_owner->p_input;
1045     vout_thread_t *p_vout = p_dec->p_owner->p_vout;
1046     input_thread_private_t *p_priv = p_input->p;
1047
1048     picture_t *p_old = NULL;
1049     picture_t *p_young = NULL;
1050     int i;
1051
1052     /* Enable with --auto-adjust-pts-delay */
1053     if( !p_priv->pts_adjust.b_auto_adjust )
1054         return;
1055
1056     for( i = 0; i < I_RENDERPICTURES; i++ )
1057     {
1058         picture_t *p_pic = PP_RENDERPICTURE[i];
1059
1060         if( p_pic->i_status != READY_PICTURE )
1061             continue;
1062
1063         if( !p_old || p_pic->date < p_old->date )
1064             p_old = p_pic;
1065         if( !p_young || p_pic->date > p_young->date )
1066             p_young = p_pic;
1067     }
1068
1069     if( !p_young || !p_old )
1070         return;
1071
1072     /* Try to find if we can reduce the pts
1073      * This first draft is way too simple, and we can't say if the
1074      * algo will converge. It's also full of constants.
1075      * But this simple algo allows to reduce the latency
1076      * to the minimum.
1077      * The whole point of this, is to bypass the pts_delay set
1078      * by the access but also the delay arbitraly set by
1079      * the remote server.
1080      * Actually the remote server's muxer may set up a
1081      * pts<->dts delay in the muxed stream. That is
1082      * why we may end up in having a negative pts_delay,
1083      * to compensate that artificial delay. */
1084     const mtime_t i_buffer_length = p_young->date - p_old->date;
1085     int64_t i_pts_slide = 0;
1086     if( i_buffer_length < 10000 )
1087     {
1088         if( p_priv->pts_adjust.i_num_faulty > 10 )
1089         {
1090             i_pts_slide = __MAX(p_input->i_pts_delay *3 / 2, 10000);
1091             p_priv->pts_adjust.i_num_faulty = 0;
1092         }
1093         if( p_priv->pts_adjust.b_to_high )
1094         {
1095             p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1096             p_priv->pts_adjust.i_num_faulty = 0;
1097         }
1098         p_priv->pts_adjust.i_num_faulty++;
1099     }
1100     else if( i_buffer_length > 100000 )
1101     {
1102         if( p_priv->pts_adjust.i_num_faulty > 25 )
1103         {
1104             i_pts_slide = -i_buffer_length/2;
1105             p_priv->pts_adjust.i_num_faulty = 0;
1106         }
1107         if( p_priv->pts_adjust.b_to_high )
1108         {
1109             p_priv->pts_adjust.b_to_high = !p_priv->pts_adjust.b_to_high;
1110             p_priv->pts_adjust.i_num_faulty = 0;
1111         }
1112         p_priv->pts_adjust.i_num_faulty++;
1113     }
1114     if( i_pts_slide != 0 )
1115     {
1116         const mtime_t i_pts_delay_org = p_input->i_pts_delay;
1117
1118         p_input->i_pts_delay += i_pts_slide;
1119
1120         /* Don't play with the pts delay for more than -2<->3sec */
1121         if( p_input->i_pts_delay < -2000000 )
1122             p_input->i_pts_delay = -2000000;
1123         else if( p_input->i_pts_delay > 3000000 )
1124             p_input->i_pts_delay = 3000000;
1125         i_pts_slide = p_input->i_pts_delay - i_pts_delay_org;
1126
1127         msg_Dbg( p_input, "Sliding the pts by %dms pts delay at %dms picture buffer was %dms",
1128             (int)i_pts_slide/1000, (int)p_input->i_pts_delay/1000, (int)i_buffer_length/1000);
1129
1130         vlc_mutex_lock( &p_vout->picture_lock );
1131         /* Slide all the picture */
1132         for( i = 0; i < I_RENDERPICTURES; i++ )
1133             PP_RENDERPICTURE[i]->date += i_pts_slide;
1134         /* FIXME: slide aout/spu */
1135         vlc_mutex_unlock( &p_vout->picture_lock );
1136     }
1137 }
1138 #endif
1139
1140 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1141 {
1142     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1143     input_thread_t *p_input = p_owner->p_input;
1144     picture_t      *p_pic;
1145     int i_lost = 0;
1146     int i_decoded = 0;
1147     int i_displayed = 0;
1148
1149     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1150     {
1151         vout_thread_t  *p_vout = p_owner->p_vout;
1152         if( p_dec->b_die )
1153         {
1154             /* It prevent freezing VLC in case of broken decoder */
1155             VoutDisplayedPicture( p_vout, p_pic );
1156             if( p_block )
1157                 block_Release( p_block );
1158             break;
1159         }
1160
1161         i_decoded++;
1162
1163         if( p_pic->date < p_owner->i_preroll_end )
1164         {
1165             VoutDisplayedPicture( p_vout, p_pic );
1166             continue;
1167         }
1168
1169         if( p_owner->i_preroll_end > 0 )
1170         {
1171             msg_Dbg( p_dec, "End of video preroll" );
1172             if( p_vout )
1173                 VoutFlushPicture( p_vout, 1 );
1174             /* */
1175             p_owner->i_preroll_end = -1;
1176         }
1177
1178         if( p_dec->pf_get_cc &&
1179             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1180             DecoderGetCc( p_dec, p_dec );
1181
1182         DecoderWaitUnpause( p_dec );
1183
1184         mtime_t i_ts_delay;
1185         mtime_t i_es_delay;
1186         DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1187         int i_rate = INPUT_RATE_DEFAULT;
1188
1189         DecoderVoutBufferFixTs( p_pic, &i_rate, p_owner->p_clock, i_ts_delay, i_es_delay );
1190
1191         /* */
1192         const mtime_t i_max_date = mdate() + i_ts_delay +
1193             i_es_delay * i_rate / INPUT_RATE_DEFAULT + VOUT_BOGUS_DELAY;
1194
1195         if( p_pic->date > 0 && p_pic->date < i_max_date )
1196         {
1197             if( i_rate != p_owner->i_last_rate  )
1198             {
1199                 /* Be sure to not display old picture after our own */
1200                 VoutFlushPicture( p_vout, p_pic->date );
1201                 p_owner->i_last_rate = i_rate;
1202             }
1203
1204             vout_DatePicture( p_vout, p_pic, p_pic->date );
1205
1206             /* Re-enable it but do it right this time */
1207             //DecoderOptimizePtsDelay( p_dec );
1208             vout_DisplayPicture( p_vout, p_pic );
1209         }
1210         else
1211         {
1212             if( p_pic->date <= 0 )
1213             {
1214                 msg_Warn( p_vout, "non-dated video buffer received" );
1215             }
1216             else
1217             {
1218                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1219                           p_pic->date - mdate() );
1220             }
1221             i_lost++;
1222             VoutDisplayedPicture( p_vout, p_pic );
1223         }
1224         int i_tmp_display;
1225         int i_tmp_lost;
1226         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1227
1228         i_displayed += i_tmp_display;
1229         i_lost += i_tmp_lost;
1230     }
1231     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1232     {
1233         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1234
1235         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1236                              i_decoded, NULL );
1237         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1238                              i_lost , NULL);
1239
1240         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1241                              i_displayed, NULL);
1242
1243         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1244     }
1245 }
1246
1247 /* This function process a block for sout
1248  */
1249 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1250 {
1251     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1252     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1253     block_t *p_sout_block;
1254
1255     while( ( p_sout_block =
1256                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1257     {
1258         if( !p_owner->p_sout_input )
1259         {
1260             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1261
1262             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1263             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1264             if( p_dec->fmt_in.psz_language )
1265             {
1266                 if( p_owner->sout.psz_language )
1267                     free( p_owner->sout.psz_language );
1268                 p_owner->sout.psz_language =
1269                     strdup( p_dec->fmt_in.psz_language );
1270             }
1271
1272             p_owner->p_sout_input =
1273                 sout_InputNew( p_owner->p_sout,
1274                                &p_owner->sout );
1275
1276             if( p_owner->p_sout_input == NULL )
1277             {
1278                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1279                          (char *)&p_owner->sout.i_codec );
1280                 p_dec->b_error = true;
1281
1282                 while( p_sout_block )
1283                 {
1284                     block_t *p_next = p_sout_block->p_next;
1285                     block_Release( p_sout_block );
1286                     p_sout_block = p_next;
1287                 }
1288                 break;
1289             }
1290         }
1291
1292         while( p_sout_block )
1293         {
1294             block_t *p_next = p_sout_block->p_next;
1295
1296             p_sout_block->p_next = NULL;
1297
1298             DecoderWaitUnpause( p_dec );
1299
1300             mtime_t i_ts_delay;
1301             mtime_t i_es_delay;
1302             DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1303
1304             DecoderSoutBufferFixTs( p_sout_block, p_owner->p_clock,
1305                                     i_ts_delay, i_es_delay, b_telx );
1306
1307             sout_InputSendBuffer( p_owner->p_sout_input,
1308                                   p_sout_block );
1309
1310             p_sout_block = p_next;
1311         }
1312
1313         /* For now it's enough, as only sout impact on this flag */
1314         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1315             p_owner->p_input->p->b_out_pace_control )
1316         {
1317             msg_Dbg( p_dec, "switching to sync mode" );
1318             p_owner->p_input->p->b_out_pace_control = false;
1319         }
1320         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1321                  !p_owner->p_input->p->b_out_pace_control )
1322         {
1323             msg_Dbg( p_dec, "switching to async mode" );
1324             p_owner->p_input->p->b_out_pace_control = true;
1325         }
1326     }
1327 }
1328
1329 /* This function process a video block
1330  */
1331 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1332 {
1333     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1334
1335     if( b_flush && p_owner->p_vout )
1336         VoutFlushPicture( p_owner->p_vout, 1 );
1337
1338     if( p_owner->p_packetizer )
1339     {
1340         block_t *p_packetized_block;
1341         decoder_t *p_packetizer = p_owner->p_packetizer;
1342
1343         while( (p_packetized_block =
1344                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1345         {
1346             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1347             {
1348                 es_format_Clean( &p_dec->fmt_in );
1349                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1350             }
1351             if( p_packetizer->pf_get_cc )
1352                 DecoderGetCc( p_dec, p_packetizer );
1353
1354             while( p_packetized_block )
1355             {
1356                 block_t *p_next = p_packetized_block->p_next;
1357                 p_packetized_block->p_next = NULL;
1358
1359                 DecoderDecodeVideo( p_dec, p_packetized_block );
1360
1361                 p_packetized_block = p_next;
1362             }
1363         }
1364     }
1365     else if( p_block )
1366     {
1367         DecoderDecodeVideo( p_dec, p_block );
1368     }
1369 }
1370
1371 /* This function process a audio block
1372  */
1373 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1374 {
1375     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1376
1377     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1378     {
1379         // TODO flush
1380     }
1381
1382     if( p_owner->p_packetizer )
1383     {
1384         block_t *p_packetized_block;
1385         decoder_t *p_packetizer = p_owner->p_packetizer;
1386
1387         while( (p_packetized_block =
1388                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1389         {
1390             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1391             {
1392                 es_format_Clean( &p_dec->fmt_in );
1393                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1394             }
1395
1396             while( p_packetized_block )
1397             {
1398                 block_t *p_next = p_packetized_block->p_next;
1399                 p_packetized_block->p_next = NULL;
1400
1401                 DecoderDecodeAudio( p_dec, p_packetized_block );
1402
1403                 p_packetized_block = p_next;
1404             }
1405         }
1406     }
1407     else if( p_block )
1408     {
1409         DecoderDecodeAudio( p_dec, p_block );
1410     }
1411 }
1412
1413 /* This function process a subtitle block
1414  */
1415 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1416 {
1417     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1418     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1419
1420     input_thread_t *p_input = p_owner->p_input;
1421     vout_thread_t *p_vout;
1422     subpicture_t *p_spu;
1423
1424     if( b_flush && p_owner->p_spu_vout )
1425     {
1426         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1427
1428         if( p_vout && p_owner->p_spu_vout == p_vout )
1429             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1430                          p_owner->i_spu_channel );
1431
1432         if( p_vout )
1433             vlc_object_release( p_vout );
1434     }
1435
1436     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1437     {
1438         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1439         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1440         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1441
1442         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1443         if( p_vout && p_owner->p_spu_vout == p_vout )
1444         {
1445             /* Preroll does not work very well with subtitle */
1446             if( p_spu->i_start > 0 &&
1447                 p_spu->i_start < p_owner->i_preroll_end &&
1448                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1449             {
1450                 subpicture_Delete( p_spu );
1451             }
1452             else
1453             {
1454                 DecoderWaitUnpause( p_dec );
1455
1456                 mtime_t i_ts_delay;
1457                 mtime_t i_es_delay;
1458                 DecoderGetDelays( p_dec, &i_ts_delay, &i_es_delay );
1459
1460                 DecoderSpuBufferFixTs( p_spu, p_owner->p_clock, i_ts_delay, i_es_delay, b_telx );
1461                 spu_DisplaySubpicture( p_vout->p_spu, p_spu );
1462             }
1463         }
1464         else
1465         {
1466             msg_Warn( p_dec, "no vout found, leaking subpicture" );
1467         }
1468         if( p_vout )
1469             vlc_object_release( p_vout );
1470     }
1471 }
1472
1473 /**
1474  * Decode a block
1475  *
1476  * \param p_dec the decoder object
1477  * \param p_block the block to decode
1478  * \return VLC_SUCCESS or an error code
1479  */
1480 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1481 {
1482     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1483
1484     if( p_block && p_block->i_buffer <= 0 )
1485     {
1486         block_Release( p_block );
1487         return VLC_SUCCESS;
1488     }
1489
1490 #ifdef ENABLE_SOUT
1491     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1492     {
1493         DecoderProcessSout( p_dec, p_block );
1494     }
1495     else
1496 #endif
1497     {
1498         bool b_flush = false;
1499
1500         if( p_block )
1501         {
1502             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1503             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1504
1505             b_flush = !b_flushing && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH) != 0;
1506
1507             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1508         }
1509
1510         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1511         {
1512             DecoderProcessAudio( p_dec, p_block, b_flush );
1513         }
1514         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1515         {
1516             DecoderProcessVideo( p_dec, p_block, b_flush );
1517         }
1518         else if( p_dec->fmt_in.i_cat == SPU_ES )
1519         {
1520             DecoderProcessSpu( p_dec, p_block, b_flush );
1521         }
1522         else
1523         {
1524             msg_Err( p_dec, "unknown ES format" );
1525             p_dec->b_error = true;
1526         }
1527     }
1528
1529     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1530 }
1531
1532 /**
1533  * Destroys a decoder object
1534  *
1535  * \param p_dec the decoder object
1536  * \return nothing
1537  */
1538 static void DeleteDecoder( decoder_t * p_dec )
1539 {
1540     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1541
1542     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1543              (char*)&p_dec->fmt_in.i_codec,
1544              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1545
1546     /* Free all packets still in the decoder fifo. */
1547     block_FifoEmpty( p_owner->p_fifo );
1548     block_FifoRelease( p_owner->p_fifo );
1549
1550     /* Cleanup */
1551     if( p_owner->p_aout_input )
1552         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1553     if( p_owner->p_aout )
1554     {
1555         vlc_object_release( p_owner->p_aout );
1556         p_owner->p_aout = NULL;
1557     }
1558     if( p_owner->p_vout )
1559     {
1560         int i_pic;
1561
1562 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1563         /* Hack to make sure all the the pictures are freed by the decoder */
1564         for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1565              i_pic++ )
1566         {
1567             if( p_pic->i_status == RESERVED_PICTURE )
1568                 vout_DestroyPicture( p_owner->p_vout, p_pic );
1569             if( p_pic->i_refcount > 0 )
1570                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1571         }
1572 #undef p_pic
1573
1574         /* We are about to die. Reattach video output to p_vlc. */
1575         vout_Request( p_dec, p_owner->p_vout, NULL );
1576         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1577     }
1578
1579 #ifdef ENABLE_SOUT
1580     if( p_owner->p_sout_input )
1581     {
1582         sout_InputDelete( p_owner->p_sout_input );
1583         es_format_Clean( &p_owner->sout );
1584     }
1585 #endif
1586
1587     if( p_dec->fmt_in.i_cat == SPU_ES )
1588     {
1589         vout_thread_t *p_vout;
1590
1591         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1592         if( p_vout && p_owner->p_spu_vout == p_vout )
1593         {
1594             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1595                          p_owner->i_spu_channel );
1596             vlc_object_release( p_vout );
1597         }
1598     }
1599
1600     es_format_Clean( &p_dec->fmt_in );
1601     es_format_Clean( &p_dec->fmt_out );
1602
1603     if( p_owner->p_packetizer )
1604     {
1605         module_unneed( p_owner->p_packetizer,
1606                        p_owner->p_packetizer->p_module );
1607         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1608         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1609         vlc_object_detach( p_owner->p_packetizer );
1610         vlc_object_release( p_owner->p_packetizer );
1611     }
1612
1613     vlc_cond_destroy( &p_owner->wait );
1614     vlc_mutex_destroy( &p_owner->lock );
1615
1616     vlc_object_detach( p_dec );
1617
1618     free( p_owner );
1619 }
1620
1621 /*****************************************************************************
1622  * Buffers allocation callbacks for the decoders
1623  *****************************************************************************/
1624 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1625 {
1626     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1627     aout_buffer_t *p_buffer;
1628
1629     if( p_owner->p_aout_input != NULL &&
1630         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1631           p_dec->fmt_out.audio.i_original_channels !=
1632               p_owner->audio.i_original_channels ||
1633           p_dec->fmt_out.audio.i_bytes_per_frame !=
1634               p_owner->audio.i_bytes_per_frame ) )
1635     {
1636         aout_input_t *p_aout_input = p_owner->p_aout_input;
1637
1638         /* Parameters changed, restart the aout */
1639         vlc_mutex_lock( &p_owner->lock );
1640
1641         p_owner->p_aout_input = NULL;
1642         aout_DecDelete( p_owner->p_aout, p_aout_input );
1643
1644         vlc_mutex_unlock( &p_owner->lock );
1645     }
1646
1647     if( p_owner->p_aout_input == NULL )
1648     {
1649         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1650         audio_sample_format_t format;
1651         aout_input_t *p_aout_input;
1652         aout_instance_t *p_aout;
1653
1654         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1655         p_owner->audio = p_dec->fmt_out.audio;
1656
1657         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1658         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1659                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1660         {
1661             if ( i_force_dolby == 1 )
1662             {
1663                 format.i_original_channels = format.i_original_channels |
1664                                              AOUT_CHAN_DOLBYSTEREO;
1665             }
1666             else /* i_force_dolby == 2 */
1667             {
1668                 format.i_original_channels = format.i_original_channels &
1669                                              ~AOUT_CHAN_DOLBYSTEREO;
1670             }
1671         }
1672
1673         p_aout = p_owner->p_aout;
1674         p_aout_input = aout_DecNew( p_dec, &p_aout,
1675                                     &format, &p_dec->fmt_out.audio_replay_gain );
1676
1677         vlc_mutex_lock( &p_owner->lock );
1678         p_owner->p_aout = p_aout;
1679         p_owner->p_aout_input = p_aout_input;
1680         vlc_mutex_unlock( &p_owner->lock );
1681
1682         if( p_owner->p_aout_input == NULL )
1683         {
1684             msg_Err( p_dec, "failed to create audio output" );
1685             p_dec->b_error = true;
1686             return NULL;
1687         }
1688         p_dec->fmt_out.audio.i_bytes_per_frame =
1689             p_owner->audio.i_bytes_per_frame;
1690     }
1691
1692     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1693
1694     return p_buffer;
1695 }
1696
1697 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1698 {
1699     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1700
1701     aout_DecDeleteBuffer( p_owner->p_aout,
1702                           p_owner->p_aout_input, p_buffer );
1703 }
1704
1705
1706 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1707
1708 static picture_t *vout_new_buffer( decoder_t *p_dec )
1709 {
1710     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1711     picture_t *p_pic;
1712
1713     if( p_owner->p_vout == NULL ||
1714         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1715         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1716         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1717         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1718     {
1719         vout_thread_t *p_vout;
1720
1721         if( !p_dec->fmt_out.video.i_width ||
1722             !p_dec->fmt_out.video.i_height )
1723         {
1724             /* Can't create a new vout without display size */
1725             return NULL;
1726         }
1727
1728         if( !p_dec->fmt_out.video.i_visible_width ||
1729             !p_dec->fmt_out.video.i_visible_height )
1730         {
1731             if( p_dec->fmt_in.video.i_visible_width &&
1732                 p_dec->fmt_in.video.i_visible_height )
1733             {
1734                 p_dec->fmt_out.video.i_visible_width =
1735                     p_dec->fmt_in.video.i_visible_width;
1736                 p_dec->fmt_out.video.i_visible_height =
1737                     p_dec->fmt_in.video.i_visible_height;
1738             }
1739             else
1740             {
1741                 p_dec->fmt_out.video.i_visible_width =
1742                     p_dec->fmt_out.video.i_width;
1743                 p_dec->fmt_out.video.i_visible_height =
1744                     p_dec->fmt_out.video.i_height;
1745             }
1746         }
1747
1748         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
1749             var_CreateGetBool( p_dec, "hdtv-fix" ) )
1750         {
1751             p_dec->fmt_out.video.i_visible_height = 1080;
1752             p_dec->fmt_out.video.i_sar_num *= 135;
1753             p_dec->fmt_out.video.i_sar_den *= 136;
1754             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
1755         }
1756
1757         if( !p_dec->fmt_out.video.i_sar_num ||
1758             !p_dec->fmt_out.video.i_sar_den )
1759         {
1760             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
1761               p_dec->fmt_out.video.i_visible_height;
1762
1763             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
1764               p_dec->fmt_out.video.i_visible_width;
1765         }
1766
1767         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
1768                      &p_dec->fmt_out.video.i_sar_den,
1769                      p_dec->fmt_out.video.i_sar_num,
1770                      p_dec->fmt_out.video.i_sar_den, 50000 );
1771
1772         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
1773         p_owner->video = p_dec->fmt_out.video;
1774
1775         vlc_mutex_lock( &p_owner->lock );
1776         p_vout = p_owner->p_vout;
1777         p_owner->p_vout = NULL;
1778         vlc_mutex_unlock( &p_owner->lock );
1779
1780         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
1781
1782         vlc_mutex_lock( &p_owner->lock );
1783         p_owner->p_vout = p_vout;
1784         vlc_mutex_unlock( &p_owner->lock );
1785
1786         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1787         if( p_vout == NULL )
1788         {
1789             msg_Err( p_dec, "failed to create video output" );
1790             p_dec->b_error = true;
1791             return NULL;
1792         }
1793
1794         if( p_owner->video.i_rmask )
1795             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
1796         if( p_owner->video.i_gmask )
1797             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
1798         if( p_owner->video.i_bmask )
1799             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
1800     }
1801
1802     /* Get a new picture
1803      */
1804     for( p_pic = NULL; ; )
1805     {
1806         int i_pic, i_ready_pic;
1807
1808         if( p_dec->b_die || p_dec->b_error )
1809             return NULL;
1810
1811         /* The video filter chain required that there is always 1 free buffer
1812          * that it will use as temporary one. It will release the temporary
1813          * buffer once its work is done, so this check is safe even if we don't
1814          * lock around both count() and create().
1815          */
1816         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
1817         {
1818             p_pic = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
1819             if( p_pic )
1820                 break;
1821         }
1822
1823 #define p_pic p_owner->p_vout->render.pp_picture[i_pic]
1824         /* Check the decoder doesn't leak pictures */
1825         for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1826         {
1827             if( p_pic->i_status == READY_PICTURE )
1828             {
1829                 i_ready_pic++;
1830                 /* If we have at least 2 ready pictures, wait for the vout thread to
1831                  * process one */
1832                 if( i_ready_pic >= 2 )
1833                     break;
1834
1835                 continue;
1836             }
1837
1838             if( p_pic->i_status == DISPLAYED_PICTURE )
1839             {
1840                 /* If at least one displayed picture is not referenced
1841                  * let vout free it */
1842                 if( p_pic->i_refcount == 0 )
1843                     break;
1844             }
1845         }
1846         if( i_pic == p_owner->p_vout->render.i_pictures )
1847         {
1848             /* Too many pictures are still referenced, there is probably a bug
1849              * with the decoder */
1850             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
1851
1852             /* Just free all the pictures */
1853             for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures;
1854                  i_pic++ )
1855             {
1856                 if( p_pic->i_status == RESERVED_PICTURE )
1857                     vout_DestroyPicture( p_owner->p_vout, p_pic );
1858                 if( p_pic->i_refcount > 0 )
1859                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1860             }
1861         }
1862 #undef p_pic
1863
1864         msleep( VOUT_OUTMEM_SLEEP );
1865     }
1866
1867     return p_pic;
1868 }
1869
1870 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
1871 {
1872     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
1873 }
1874
1875 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
1876 {
1877     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
1878 }
1879
1880 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
1881 {
1882     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
1883 }
1884
1885 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
1886 {
1887     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1888     vout_thread_t *p_vout = NULL;
1889     subpicture_t *p_subpic;
1890     int i_attempts = 30;
1891
1892     while( i_attempts-- )
1893     {
1894         if( p_dec->b_die || p_dec->b_error )
1895             break;
1896
1897         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1898         if( p_vout )
1899             break;
1900
1901         msleep( VOUT_DISPLAY_DELAY );
1902     }
1903
1904     if( !p_vout )
1905     {
1906         msg_Warn( p_dec, "no vout found, dropping subpicture" );
1907         return NULL;
1908     }
1909
1910     if( p_owner->p_spu_vout != p_vout )
1911     {
1912         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
1913                      &p_owner->i_spu_channel );
1914         p_owner->i_spu_order = 0;
1915         p_owner->p_spu_vout = p_vout;
1916     }
1917
1918     p_subpic = subpicture_New();
1919     if( p_subpic )
1920     {
1921         p_subpic->i_channel = p_owner->i_spu_channel;
1922         p_subpic->i_order = p_owner->i_spu_order++;
1923         p_subpic->b_subtitle = true;
1924     }
1925
1926     vlc_object_release( p_vout );
1927
1928     return p_subpic;
1929 }
1930
1931 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
1932 {
1933     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1934     vout_thread_t *p_vout = NULL;
1935
1936     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1937     if( !p_vout || p_owner->p_spu_vout != p_vout )
1938     {
1939         if( p_vout )
1940             vlc_object_release( p_vout );
1941         msg_Warn( p_dec, "no vout found, leaking subpicture" );
1942         return;
1943     }
1944
1945     subpicture_Delete( p_subpic );
1946
1947     vlc_object_release( p_vout );
1948 }
1949