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