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