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