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