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