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