]> git.sesse.net Git - vlc/blob - src/input/decoder.c
Prefer new frames over flushed one (vout).
[vlc] / src / input / decoder.c
1 /*****************************************************************************
2  * decoder.c: Functions for the management of decoders
3  *****************************************************************************
4  * Copyright (C) 1999-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Christophe Massiot <massiot@via.ecp.fr>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Laurent Aimar <fenrir@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35
36 #include <vlc_block.h>
37 #include <vlc_vout.h>
38 #include <vlc_aout.h>
39 #include <vlc_sout.h>
40 #include <vlc_codec.h>
41 #include <vlc_osd.h>
42
43 #include <vlc_interface.h>
44 #include "audio_output/aout_internal.h"
45 #include "stream_output/stream_output.h"
46 #include "input_internal.h"
47 #include "input_clock.h"
48 #include "input_decoder.h"
49
50 #include "../video_output/vout_internal.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_full;
128         int  i_count;
129
130         picture_t     *p_picture;
131         picture_t     **pp_picture_next;
132
133         subpicture_t  *p_subpic;
134         subpicture_t  **pp_subpic_next;
135
136         aout_buffer_t *p_audio;
137         aout_buffer_t **pp_audio_next;
138     } buffer;
139
140     /* Flushing */
141     bool b_flushing;
142
143     /* CC */
144     struct
145     {
146         bool b_supported;
147         bool pb_present[4];
148         decoder_t *pp_decoder[4];
149     } cc;
150
151     /* Delay */
152     mtime_t i_ts_delay;
153 };
154
155 #define DECODER_MAX_BUFFERING_COUNT (4)
156 #define DECODER_MAX_BUFFERING_AUDIO_DURATION (AOUT_MAX_PREPARE_TIME)
157 #define DECODER_MAX_BUFFERING_VIDEO_DURATION (1*CLOCK_FREQ)
158
159 /*****************************************************************************
160  * Public functions
161  *****************************************************************************/
162
163 /* decoder_GetInputAttachment:
164  */
165 input_attachment_t *decoder_GetInputAttachment( decoder_t *p_dec,
166                                                 const char *psz_name )
167 {
168     input_attachment_t *p_attachment;
169     if( input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENT, &p_attachment, psz_name ) )
170         return NULL;
171     return p_attachment;
172 }
173 /* decoder_GetInputAttachments:
174  */
175 int decoder_GetInputAttachments( decoder_t *p_dec,
176                                  input_attachment_t ***ppp_attachment,
177                                  int *pi_attachment )
178 {
179     return input_Control( p_dec->p_owner->p_input, INPUT_GET_ATTACHMENTS,
180                           ppp_attachment, pi_attachment );
181 }
182 /* decoder_GetDisplayDate:
183  */
184 mtime_t decoder_GetDisplayDate( decoder_t *p_dec, mtime_t i_ts )
185 {
186     decoder_owner_sys_t *p_owner = p_dec->p_owner;
187
188     vlc_mutex_lock( &p_owner->lock );
189     if( p_owner->b_buffering )
190         i_ts = 0;
191     vlc_mutex_unlock( &p_owner->lock );
192
193     if( !p_owner->p_clock || !i_ts )
194         return i_ts;
195
196     return input_clock_GetTS( p_owner->p_clock, NULL, p_owner->p_input->i_pts_delay, i_ts );
197 }
198 /* decoder_GetDisplayRate:
199  */
200 int decoder_GetDisplayRate( decoder_t *p_dec )
201 {
202     decoder_owner_sys_t *p_owner = p_dec->p_owner;
203
204     if( !p_owner->p_clock )
205         return INPUT_RATE_DEFAULT;
206     return input_clock_GetRate( p_owner->p_clock );
207 }
208
209 /**
210  * Spawns a new decoder thread
211  *
212  * \param p_input the input thread
213  * \param p_es the es descriptor
214  * \return the spawned decoder object
215  */
216 decoder_t *input_DecoderNew( input_thread_t *p_input,
217                              es_format_t *fmt, input_clock_t *p_clock, sout_instance_t *p_sout  )
218 {
219     decoder_t *p_dec = NULL;
220     int i_priority;
221
222 #ifndef ENABLE_SOUT
223     (void)b_force_decoder;
224 #else
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 ) && !p_dec->b_error &&
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     vlc_cond_signal( &p_owner->wait );
463
464     DecoderOutputChangePause( p_dec, b_paused, i_date );
465
466     vlc_mutex_unlock( &p_owner->lock );
467 }
468
469 void input_DecoderChangeDelay( decoder_t *p_dec, mtime_t i_delay )
470 {
471     decoder_owner_sys_t *p_owner = p_dec->p_owner;
472
473     vlc_mutex_lock( &p_owner->lock );
474     p_owner->i_ts_delay = i_delay;
475     vlc_mutex_unlock( &p_owner->lock );
476 }
477
478 void input_DecoderStartBuffering( decoder_t *p_dec )
479 {
480     decoder_owner_sys_t *p_owner = p_dec->p_owner;
481
482     vlc_mutex_lock( &p_owner->lock );
483
484     DecoderFlush( p_dec );
485
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_full = false;
685     p_owner->buffer.i_count = 0;
686     p_owner->buffer.p_picture = NULL;
687     p_owner->buffer.p_subpic = NULL;
688     p_owner->buffer.p_audio = NULL;
689
690     p_owner->b_flushing = false;
691
692     /* */
693     p_owner->cc.b_supported = false;
694     if( i_object_type == VLC_OBJECT_DECODER )
695     {
696         if( p_owner->p_packetizer && p_owner->p_packetizer->pf_get_cc )
697             p_owner->cc.b_supported = true;
698         if( p_dec->pf_get_cc )
699             p_owner->cc.b_supported = true;
700     }
701
702     for( i = 0; i < 4; i++ )
703     {
704         p_owner->cc.pb_present[i] = false;
705         p_owner->cc.pp_decoder[i] = NULL;
706     }
707     p_owner->i_ts_delay = 0;
708     return p_dec;
709 }
710
711 /**
712  * The decoding main loop
713  *
714  * \param p_dec the decoder
715  */
716 static void *DecoderThread( vlc_object_t *p_this )
717 {
718     decoder_t *p_dec = (decoder_t *)p_this;
719     decoder_owner_sys_t *p_owner = p_dec->p_owner;
720
721     int canc = vlc_savecancel();
722
723     /* The decoder's main loop */
724     while( vlc_object_alive( p_dec ) && !p_dec->b_error )
725     {
726         block_t *p_block = block_FifoGet( p_owner->p_fifo );
727
728         DecoderSignalBuffering( p_dec, p_block == NULL );
729
730         if( p_block && DecoderProcess( p_dec, p_block ) != VLC_SUCCESS )
731             break;
732     }
733
734     while( vlc_object_alive( p_dec ) )
735     {
736         block_t *p_block = block_FifoGet( p_owner->p_fifo );
737
738         DecoderSignalBuffering( p_dec, p_block == NULL );
739
740         /* Trash all received PES packets */
741         if( p_block )
742             block_Release( p_block );
743     }
744     DecoderSignalBuffering( p_dec, true );
745
746     /* We do it here because of the dll loader that wants close() in the
747      * same thread than open()/decode() */
748     module_unneed( p_dec, p_dec->p_module );
749     vlc_restorecancel( canc );
750     return NULL;
751 }
752
753 static void DecoderFlush( decoder_t *p_dec )
754 {
755     decoder_owner_sys_t *p_owner = p_dec->p_owner;
756     block_t *p_null;
757
758     vlc_assert_locked( &p_owner->lock );
759
760     /* Empty the fifo */
761     block_FifoEmpty( p_owner->p_fifo );
762
763     /* Monitor for flush end */
764     p_owner->b_flushing = true;
765     vlc_cond_signal( &p_owner->wait );
766
767     /* Send a special block */
768     p_null = block_New( p_dec, 128 );
769     if( !p_null )
770         return;
771     p_null->i_flags |= BLOCK_FLAG_DISCONTINUITY;
772     p_null->i_flags |= BLOCK_FLAG_CORE_FLUSH;
773     if( !p_dec->fmt_in.b_packetized )
774         p_null->i_flags |= BLOCK_FLAG_CORRUPTED;
775     memset( p_null->p_buffer, 0, p_null->i_buffer );
776
777     input_DecoderDecode( p_dec, p_null );
778
779     /* */
780     while( vlc_object_alive( p_dec ) && p_owner->b_flushing )
781         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
782 }
783
784 static void DecoderSignalFlushed( decoder_t *p_dec )
785 {
786     decoder_owner_sys_t *p_owner = p_dec->p_owner;
787
788     vlc_mutex_lock( &p_owner->lock );
789
790     if( p_owner->b_flushing )
791     {
792         p_owner->b_flushing = false;
793         vlc_cond_signal( &p_owner->wait );
794     }
795
796     vlc_mutex_unlock( &p_owner->lock );
797 }
798
799 static void DecoderSignalBuffering( decoder_t *p_dec, bool b_full )
800 {
801     decoder_owner_sys_t *p_owner = p_dec->p_owner;
802
803     vlc_mutex_lock( &p_owner->lock );
804
805     if( p_owner->b_buffering )
806     {
807         if( b_full )
808             p_owner->buffer.b_full = true;
809         vlc_cond_signal( &p_owner->wait );
810     }
811
812     vlc_mutex_unlock( &p_owner->lock );
813 }
814
815 static bool DecoderIsFlushing( decoder_t *p_dec )
816 {
817     decoder_owner_sys_t *p_owner = p_dec->p_owner;
818     bool b_flushing;
819
820     vlc_mutex_lock( &p_owner->lock );
821
822     b_flushing = p_owner->b_flushing;
823
824     vlc_mutex_unlock( &p_owner->lock );
825
826     return b_flushing;
827 }
828
829 static void DecoderWaitUnblock( decoder_t *p_dec, bool *pb_reject )
830 {
831     decoder_owner_sys_t *p_owner = p_dec->p_owner;
832
833     vlc_assert_locked( &p_owner->lock );
834
835     while( !p_owner->b_flushing )
836     {
837         if( !p_owner->b_paused && ( !p_owner->b_buffering || !p_owner->buffer.b_full ) )
838             break;
839         vlc_cond_wait( &p_owner->wait, &p_owner->lock );
840     }
841
842     if( pb_reject )
843         *pb_reject = p_owner->b_flushing;
844 }
845
846 static void DecoderOutputChangePause( decoder_t *p_dec, bool b_paused, mtime_t i_date )
847 {
848     decoder_owner_sys_t *p_owner = p_dec->p_owner;
849
850     vlc_assert_locked( &p_owner->lock );
851
852     /* XXX only audio and video output have to be paused.
853      * - for sout it is useless
854      * - for subs, it is done by the vout
855      */
856     if( p_dec->fmt_in.i_cat == AUDIO_ES )
857     {
858         if( p_owner->p_aout && p_owner->p_aout_input )
859             aout_DecChangePause( p_owner->p_aout, p_owner->p_aout_input,
860                                  b_paused, i_date );
861     }
862     else if( p_dec->fmt_in.i_cat == VIDEO_ES )
863     {
864         if( p_owner->p_vout )
865             vout_ChangePause( p_owner->p_vout, b_paused, i_date );
866     }
867 }
868 static inline void DecoderUpdatePreroll( int64_t *pi_preroll, const block_t *p )
869 {
870     if( p->i_flags & (BLOCK_FLAG_PREROLL|BLOCK_FLAG_DISCONTINUITY) )
871         *pi_preroll = INT64_MAX;
872     else if( p->i_pts > 0 )
873         *pi_preroll = __MIN( *pi_preroll, p->i_pts );
874     else if( p->i_dts > 0 )
875         *pi_preroll = __MIN( *pi_preroll, p->i_dts );
876 }
877
878 static mtime_t DecoderTeletextFixTs( mtime_t i_ts, mtime_t i_ts_delay )
879 {
880     mtime_t current_date = mdate();
881
882     /* FIXME I don't really like that, es_out SHOULD do it using the video pts */
883     if( !i_ts || i_ts > current_date + 10000000 || i_ts < current_date )
884     {
885         /* ETSI EN 300 472 Annex A : do not take into account the PTS
886          * for teletext streams. */
887         return current_date + 400000 + i_ts_delay;
888     }
889     return i_ts;
890 }
891
892 static void DecoderFixTs( decoder_t *p_dec, mtime_t *pi_ts0, mtime_t *pi_ts1,
893                           mtime_t *pi_duration, int *pi_rate, mtime_t *pi_delay, bool b_telx )
894 {
895     decoder_owner_sys_t *p_owner = p_dec->p_owner;
896     input_clock_t   *p_clock = p_owner->p_clock;
897     int i_rate = 0;
898
899     vlc_assert_locked( &p_owner->lock );
900
901     const mtime_t i_ts_delay = p_owner->p_input->i_pts_delay;
902     const mtime_t i_es_delay = p_owner->i_ts_delay;
903
904     if( p_clock )
905     {
906         const bool b_ephemere = pi_ts1 && *pi_ts0 == *pi_ts1;
907
908         if( *pi_ts0 > 0 )
909             *pi_ts0 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
910                                          *pi_ts0 + i_es_delay );
911         if( pi_ts1 && *pi_ts1 > 0 )
912             *pi_ts1 = input_clock_GetTS( p_clock, &i_rate, i_ts_delay,
913                                          *pi_ts1 + i_es_delay );
914
915         /* Do not create ephemere data because of rounding errors */
916         if( !b_ephemere && pi_ts1 && *pi_ts0 == *pi_ts1 )
917             *pi_ts1 += 1;
918
919         if( i_rate <= 0 )
920             i_rate = input_clock_GetRate( p_clock ); 
921
922         if( pi_duration )
923             *pi_duration = ( *pi_duration * i_rate +
924                                     INPUT_RATE_DEFAULT-1 ) / INPUT_RATE_DEFAULT;
925
926         if( pi_rate )
927             *pi_rate = i_rate;
928
929         if( b_telx )
930         {
931             *pi_ts0 = DecoderTeletextFixTs( *pi_ts0, i_ts_delay );
932             if( pi_ts1 && *pi_ts1 <= 0 )
933                 *pi_ts1 = *pi_ts0;
934         }
935     }
936     if( pi_delay )
937     {
938         const int r = i_rate > 0 ? i_rate : INPUT_RATE_DEFAULT;
939         *pi_delay = i_ts_delay + i_es_delay * r / INPUT_RATE_DEFAULT;
940     }
941 }
942
943 static void DecoderPlayAudio( decoder_t *p_dec, aout_buffer_t *p_audio,
944                               int *pi_played_sum, int *pi_lost_sum )
945 {
946     decoder_owner_sys_t *p_owner = p_dec->p_owner;
947     aout_instance_t *p_aout = p_owner->p_aout;
948     aout_input_t    *p_aout_input = p_owner->p_aout_input;
949
950     /* */
951     if( p_audio->start_date <= 0 )
952     {
953         msg_Warn( p_dec, "non-dated audio buffer received" );
954         *pi_lost_sum += 1;
955         aout_BufferFree( p_audio );
956         return;
957     }
958
959     /* */
960     vlc_mutex_lock( &p_owner->lock );
961
962     if( p_owner->b_buffering || p_owner->buffer.p_audio )
963     {
964         p_audio->p_next = NULL;
965
966         *p_owner->buffer.pp_audio_next = p_audio;
967         p_owner->buffer.pp_audio_next = &p_audio->p_next;
968
969         p_owner->buffer.i_count++;
970         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
971             p_audio->start_date - p_owner->buffer.p_audio->start_date > DECODER_MAX_BUFFERING_AUDIO_DURATION )
972         {
973             p_owner->buffer.b_full = true;
974             vlc_cond_signal( &p_owner->wait );
975         }
976     }
977
978     for( ;; )
979     {
980         bool b_has_more = false;
981         bool b_reject;
982         DecoderWaitUnblock( p_dec, &b_reject );
983
984         if( p_owner->b_buffering )
985         {
986             vlc_mutex_unlock( &p_owner->lock );
987             return;
988         }
989
990         /* */
991         if( p_owner->buffer.p_audio )
992         {
993             p_audio = p_owner->buffer.p_audio;
994
995             p_owner->buffer.p_audio = p_audio->p_next;
996
997             b_has_more = p_owner->buffer.p_audio != NULL;
998             if( !b_has_more )
999                 p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1000         }
1001
1002         /* */
1003         int i_rate = INPUT_RATE_DEFAULT;
1004         mtime_t i_delay;
1005
1006         DecoderFixTs( p_dec, &p_audio->start_date, &p_audio->end_date, NULL,
1007                       &i_rate, &i_delay, false );
1008
1009         vlc_mutex_unlock( &p_owner->lock );
1010
1011         /* */
1012         const mtime_t i_max_date = mdate() + i_delay + AOUT_MAX_ADVANCE_TIME;
1013
1014         if( !p_aout || !p_aout_input ||
1015             p_audio->start_date <= 0 || p_audio->start_date > i_max_date ||
1016             i_rate < INPUT_RATE_DEFAULT/AOUT_MAX_INPUT_RATE ||
1017             i_rate > INPUT_RATE_DEFAULT*AOUT_MAX_INPUT_RATE )
1018             b_reject = true;
1019
1020         if( !b_reject )
1021         {
1022             /* Wait if we are too early
1023              * FIXME that's plain ugly to do it here */
1024             mwait( p_audio->start_date - AOUT_MAX_PREPARE_TIME );
1025
1026             if( !aout_DecPlay( p_aout, p_aout_input, p_audio, i_rate ) )
1027                 *pi_played_sum += 1;
1028             *pi_lost_sum += aout_DecGetResetLost( p_aout, p_aout_input );
1029         }
1030         else
1031         {
1032             if( p_audio->start_date <= 0 )
1033             {
1034                 msg_Warn( p_dec, "non-dated audio buffer received" );
1035             }
1036             else if( p_audio->start_date > i_max_date )
1037             {
1038                 msg_Warn( p_aout, "received buffer in the future (%"PRId64")",
1039                           p_audio->start_date - mdate() );
1040             }
1041             *pi_lost_sum += 1;
1042             aout_BufferFree( p_audio );
1043         }
1044
1045         if( !b_has_more )
1046             break;
1047         vlc_mutex_lock( &p_owner->lock );
1048     }
1049 }
1050
1051 static void DecoderDecodeAudio( decoder_t *p_dec, block_t *p_block )
1052 {
1053     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1054     input_thread_t  *p_input = p_owner->p_input;
1055     aout_buffer_t   *p_aout_buf;
1056     int i_decoded = 0;
1057     int i_lost = 0;
1058     int i_played = 0;
1059
1060     while( (p_aout_buf = p_dec->pf_decode_audio( p_dec, &p_block )) )
1061     {
1062         aout_instance_t *p_aout = p_owner->p_aout;
1063         aout_input_t    *p_aout_input = p_owner->p_aout_input;
1064         int i_lost = 0;
1065         int i_played;
1066
1067         if( p_dec->b_die )
1068         {
1069             /* It prevent freezing VLC in case of broken decoder */
1070             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1071             if( p_block )
1072                 block_Release( p_block );
1073             break;
1074         }
1075         i_decoded++;
1076
1077         if( p_aout_buf->start_date < p_owner->i_preroll_end )
1078         {
1079             aout_DecDeleteBuffer( p_aout, p_aout_input, p_aout_buf );
1080             continue;
1081         }
1082
1083         if( p_owner->i_preroll_end > 0 )
1084         {
1085             msg_Dbg( p_dec, "End of audio preroll" );
1086             if( p_owner->p_aout && p_owner->p_aout_input )
1087                 aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1088             /* */
1089             p_owner->i_preroll_end = -1;
1090         }
1091
1092         DecoderPlayAudio( p_dec, p_aout_buf, &i_played, &i_lost );
1093     }
1094
1095     /* Update ugly stat */
1096     if( i_decoded > 0 || i_lost > 0 || i_played > 0 )
1097     {
1098         vlc_mutex_lock( &p_input->p->counters.counters_lock);
1099
1100         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_abuffers,
1101                              i_lost, NULL );
1102         stats_UpdateInteger( p_dec, p_input->p->counters.p_played_abuffers,
1103                              i_played, NULL );
1104         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_audio,
1105                              i_decoded, NULL );
1106
1107         vlc_mutex_unlock( &p_input->p->counters.counters_lock);
1108     }
1109 }
1110 static void DecoderGetCc( decoder_t *p_dec, decoder_t *p_dec_cc )
1111 {
1112     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1113     block_t *p_cc;
1114     bool pb_present[4];
1115     int i;
1116     int i_cc_decoder;
1117
1118     assert( p_dec_cc->pf_get_cc != NULL );
1119
1120     /* Do not try retreiving CC if not wanted (sout) or cannot be retreived */
1121     if( !p_owner->cc.b_supported )
1122         return;
1123
1124     p_cc = p_dec_cc->pf_get_cc( p_dec_cc, pb_present );
1125     if( !p_cc )
1126         return;
1127
1128     vlc_mutex_lock( &p_owner->lock );
1129     for( i = 0, i_cc_decoder = 0; i < 4; i++ )
1130     {
1131         p_owner->cc.pb_present[i] |= pb_present[i];
1132         if( p_owner->cc.pp_decoder[i] )
1133             i_cc_decoder++;
1134     }
1135
1136     for( i = 0; i < 4; i++ )
1137     {
1138         if( !p_owner->cc.pp_decoder[i] )
1139             continue;
1140
1141         if( i_cc_decoder > 1 )
1142             DecoderProcess( p_owner->cc.pp_decoder[i], block_Duplicate( p_cc ) );
1143         else
1144             DecoderProcess( p_owner->cc.pp_decoder[i], p_cc );
1145         i_cc_decoder--;
1146     }
1147     vlc_mutex_unlock( &p_owner->lock );
1148 }
1149 static void VoutDisplayedPicture( vout_thread_t *p_vout, picture_t *p_pic )
1150 {
1151     vlc_mutex_lock( &p_vout->picture_lock );
1152
1153     if( p_pic->i_status == READY_PICTURE )
1154     {
1155         /* Grr cannot destroy ready picture by myself so be sure vout won't like it */
1156         p_pic->date = 1;
1157     }
1158     else if( p_pic->i_refcount > 0 )
1159     {
1160         p_pic->i_status = DISPLAYED_PICTURE;
1161     }
1162     else
1163     {
1164         p_pic->i_status = DESTROYED_PICTURE;
1165         picture_CleanupQuant( p_pic );
1166         p_vout->i_heap_size--;
1167     }
1168
1169     vlc_mutex_unlock( &p_vout->picture_lock );
1170 }
1171 static void VoutFlushPicture( vout_thread_t *p_vout, mtime_t i_max_date )
1172 {
1173     int i;
1174     vlc_mutex_lock( &p_vout->picture_lock );
1175     for( i = 0; i < p_vout->render.i_pictures; i++ )
1176     {
1177         picture_t *p_pic = p_vout->render.pp_picture[i];
1178
1179         if( p_pic->i_status == READY_PICTURE ||
1180             p_pic->i_status == DISPLAYED_PICTURE )
1181         {
1182             /* We cannot change picture status if it is in READY_PICTURE state,
1183              * Just make sure they won't be displayed */
1184             if( p_pic->date > i_max_date )
1185                 p_pic->date = i_max_date;
1186         }
1187     }
1188     vlc_mutex_unlock( &p_vout->picture_lock );
1189 }
1190
1191 static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
1192                               int *pi_played_sum, int *pi_lost_sum )
1193 {
1194     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1195     vout_thread_t  *p_vout = p_owner->p_vout;
1196
1197     if( p_picture->date <= 0 )
1198     {
1199         msg_Warn( p_vout, "non-dated video buffer received" );
1200         *pi_lost_sum += 1;
1201         VoutDisplayedPicture( p_vout, p_picture );
1202         return;
1203     }
1204
1205     /* */
1206     vlc_mutex_lock( &p_owner->lock );
1207
1208     if( p_owner->b_buffering || p_owner->buffer.p_picture )
1209     {
1210         p_picture->p_next = NULL;
1211
1212         *p_owner->buffer.pp_picture_next = p_picture;
1213         p_owner->buffer.pp_picture_next = &p_picture->p_next;
1214
1215         p_owner->buffer.i_count++;
1216         if( p_owner->buffer.i_count > DECODER_MAX_BUFFERING_COUNT ||
1217             p_picture->date - p_owner->buffer.p_picture->date > DECODER_MAX_BUFFERING_VIDEO_DURATION )
1218         {
1219             p_owner->buffer.b_full = true;
1220             vlc_cond_signal( &p_owner->wait );
1221         }
1222     }
1223
1224     for( ;; )
1225     {
1226         bool b_has_more = false;
1227
1228         bool b_reject;
1229         DecoderWaitUnblock( p_dec, &b_reject );
1230
1231         if( p_owner->b_buffering )
1232         {
1233             vlc_mutex_unlock( &p_owner->lock );
1234             return;
1235         }
1236
1237         /* */
1238         if( p_owner->buffer.p_picture )
1239         {
1240             p_picture = p_owner->buffer.p_picture;
1241
1242             p_owner->buffer.p_picture = p_picture->p_next;
1243
1244             b_has_more = p_owner->buffer.p_picture != NULL;
1245             if( !b_has_more )
1246                 p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1247         }
1248
1249         /* */
1250         int i_rate = INPUT_RATE_DEFAULT;
1251         mtime_t i_delay;
1252
1253         DecoderFixTs( p_dec, &p_picture->date, NULL, NULL,
1254                       &i_rate, &i_delay, false );
1255
1256         vlc_mutex_unlock( &p_owner->lock );
1257
1258         /* */
1259         const mtime_t i_max_date = mdate() + i_delay + VOUT_BOGUS_DELAY;
1260
1261         if( p_picture->date <= 0 || p_picture->date >= i_max_date )
1262             b_reject = true;
1263
1264         if( !b_reject )
1265         {
1266             if( i_rate != p_owner->i_last_rate  )
1267             {
1268                 /* Be sure to not display old picture after our own */
1269                 VoutFlushPicture( p_vout, p_picture->date );
1270                 p_owner->i_last_rate = i_rate;
1271             }
1272
1273             vout_DatePicture( p_vout, p_picture, p_picture->date );
1274
1275             vout_DisplayPicture( p_vout, p_picture );
1276         }
1277         else
1278         {
1279             if( p_picture->date <= 0 )
1280             {
1281                 msg_Warn( p_vout, "non-dated video buffer received" );
1282             }
1283             else
1284             {
1285                 msg_Warn( p_vout, "early picture skipped (%"PRId64")",
1286                           p_picture->date - mdate() );
1287             }
1288             *pi_lost_sum += 1;
1289             VoutDisplayedPicture( p_vout, p_picture );
1290         }
1291         int i_tmp_display;
1292         int i_tmp_lost;
1293         vout_GetResetStatistic( p_vout, &i_tmp_display, &i_tmp_lost );
1294
1295         *pi_played_sum += i_tmp_display;
1296         *pi_lost_sum += i_tmp_lost;
1297
1298         if( !b_has_more )
1299             break;
1300         vlc_mutex_lock( &p_owner->lock );
1301     }
1302 }
1303
1304 static void DecoderDecodeVideo( decoder_t *p_dec, block_t *p_block )
1305 {
1306     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1307     input_thread_t *p_input = p_owner->p_input;
1308     picture_t      *p_pic;
1309     int i_lost = 0;
1310     int i_decoded = 0;
1311     int i_displayed = 0;
1312
1313     while( (p_pic = p_dec->pf_decode_video( p_dec, &p_block )) )
1314     {
1315         vout_thread_t  *p_vout = p_owner->p_vout;
1316         if( p_dec->b_die )
1317         {
1318             /* It prevent freezing VLC in case of broken decoder */
1319             VoutDisplayedPicture( p_vout, p_pic );
1320             if( p_block )
1321                 block_Release( p_block );
1322             break;
1323         }
1324
1325         i_decoded++;
1326
1327         if( p_pic->date < p_owner->i_preroll_end )
1328         {
1329             VoutDisplayedPicture( p_vout, p_pic );
1330             continue;
1331         }
1332
1333         if( p_owner->i_preroll_end > 0 )
1334         {
1335             msg_Dbg( p_dec, "End of video preroll" );
1336             if( p_vout )
1337                 VoutFlushPicture( p_vout, 1 );
1338             /* */
1339             p_owner->i_preroll_end = -1;
1340         }
1341
1342         if( p_dec->pf_get_cc &&
1343             ( !p_owner->p_packetizer || !p_owner->p_packetizer->pf_get_cc ) )
1344             DecoderGetCc( p_dec, p_dec );
1345
1346         DecoderPlayVideo( p_dec, p_pic, &i_displayed, &i_lost );
1347     }
1348     if( i_decoded > 0 || i_lost > 0 || i_displayed > 0 )
1349     {
1350         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1351
1352         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_video,
1353                              i_decoded, NULL );
1354         stats_UpdateInteger( p_dec, p_input->p->counters.p_lost_pictures,
1355                              i_lost , NULL);
1356
1357         stats_UpdateInteger( p_dec, p_input->p->counters.p_displayed_pictures,
1358                              i_displayed, NULL);
1359
1360         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1361     }
1362 }
1363
1364 static void DecoderPlaySpu( decoder_t *p_dec, subpicture_t *p_subpic,
1365                             bool b_telx )
1366 {
1367     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1368     vout_thread_t *p_vout = p_owner->p_spu_vout;
1369
1370     /* */
1371     if( p_subpic->i_start <= 0 )
1372     {
1373         msg_Warn( p_dec, "non-dated spu buffer received" );
1374         subpicture_Delete( p_subpic );
1375         return;
1376     }
1377
1378     /* */
1379     vlc_mutex_lock( &p_owner->lock );
1380
1381     if( p_owner->b_buffering || p_owner->buffer.p_subpic )
1382     {
1383         p_subpic->p_next = NULL;
1384
1385         *p_owner->buffer.pp_subpic_next = p_subpic;
1386         p_owner->buffer.pp_subpic_next = &p_subpic->p_next;
1387
1388         p_owner->buffer.i_count++;
1389         /* XXX it is important to be full after the first one */
1390         if( p_owner->buffer.i_count > 0 )
1391         {
1392             p_owner->buffer.b_full = true;
1393             vlc_cond_signal( &p_owner->wait );
1394         }
1395     }
1396
1397     for( ;; )
1398     {
1399         bool b_has_more = false;
1400         bool b_reject;
1401         DecoderWaitUnblock( p_dec, &b_reject );
1402
1403         if( p_owner->b_buffering )
1404         {
1405             vlc_mutex_unlock( &p_owner->lock );
1406             return;
1407         }
1408
1409         /* */
1410         if( p_owner->buffer.p_subpic )
1411         {
1412             p_subpic = p_owner->buffer.p_subpic;
1413
1414             p_owner->buffer.p_subpic = p_subpic->p_next;
1415
1416             b_has_more = p_owner->buffer.p_subpic != NULL;
1417             if( !b_has_more )
1418                 p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1419         }
1420
1421         /* */
1422         DecoderFixTs( p_dec, &p_subpic->i_start, &p_subpic->i_stop, NULL,
1423                       NULL, NULL, b_telx );
1424
1425         vlc_mutex_unlock( &p_owner->lock );
1426
1427         if( !b_reject )
1428             spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
1429         else
1430             subpicture_Delete( p_subpic );
1431
1432         if( !b_has_more )
1433             break;
1434         vlc_mutex_lock( &p_owner->lock );
1435     }
1436 }
1437
1438 static void DecoderPlaySout( decoder_t *p_dec, block_t *p_sout_block,
1439                              bool b_telx )
1440 {
1441     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1442
1443     assert( p_owner->p_clock );
1444
1445     vlc_mutex_lock( &p_owner->lock );
1446
1447     bool b_reject;
1448     DecoderWaitUnblock( p_dec, &b_reject );
1449
1450     DecoderFixTs( p_dec, &p_sout_block->i_dts, &p_sout_block->i_pts, &p_sout_block->i_length,
1451                   &p_sout_block->i_rate, NULL, b_telx );
1452
1453     vlc_mutex_unlock( &p_owner->lock );
1454
1455     sout_InputSendBuffer( p_owner->p_sout_input, p_sout_block );
1456 }
1457
1458 /* */
1459 static void DecoderFlushBuffering( decoder_t *p_dec )
1460 {
1461     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1462
1463     vlc_assert_locked( &p_owner->lock );
1464
1465     while( p_owner->buffer.p_picture )
1466     {
1467         picture_t *p_picture = p_owner->buffer.p_picture;
1468
1469         p_owner->buffer.p_picture = p_picture->p_next;
1470         p_owner->buffer.i_count--;
1471
1472         if( p_owner->p_vout )
1473             VoutDisplayedPicture( p_owner->p_vout, p_picture );
1474
1475         if( !p_owner->buffer.p_picture )
1476             p_owner->buffer.pp_picture_next = &p_owner->buffer.p_picture;
1477     }
1478     while( p_owner->buffer.p_audio )
1479     {
1480         aout_buffer_t *p_audio = p_owner->buffer.p_audio;
1481
1482         p_owner->buffer.p_audio = p_audio->p_next;
1483         p_owner->buffer.i_count--;
1484
1485         aout_BufferFree( p_audio );
1486
1487         if( !p_owner->buffer.p_audio )
1488             p_owner->buffer.pp_audio_next = &p_owner->buffer.p_audio;
1489     }
1490     while( p_owner->buffer.p_subpic )
1491     {
1492         subpicture_t *p_subpic = p_owner->buffer.p_subpic;
1493
1494         p_owner->buffer.p_subpic = p_subpic->p_next;
1495         p_owner->buffer.i_count--;
1496
1497         subpicture_Delete( p_subpic );
1498
1499         if( !p_owner->buffer.p_subpic )
1500             p_owner->buffer.pp_subpic_next = &p_owner->buffer.p_subpic;
1501     }
1502 }
1503
1504 /* This function process a block for sout
1505  */
1506 static void DecoderProcessSout( decoder_t *p_dec, block_t *p_block )
1507 {
1508     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1509     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1510     block_t *p_sout_block;
1511
1512     while( ( p_sout_block =
1513                  p_dec->pf_packetize( p_dec, p_block ? &p_block : NULL ) ) )
1514     {
1515         if( !p_owner->p_sout_input )
1516         {
1517             es_format_Copy( &p_owner->sout, &p_dec->fmt_out );
1518
1519             p_owner->sout.i_group = p_dec->fmt_in.i_group;
1520             p_owner->sout.i_id = p_dec->fmt_in.i_id;
1521             if( p_dec->fmt_in.psz_language )
1522             {
1523                 if( p_owner->sout.psz_language )
1524                     free( p_owner->sout.psz_language );
1525                 p_owner->sout.psz_language =
1526                     strdup( p_dec->fmt_in.psz_language );
1527             }
1528
1529             p_owner->p_sout_input =
1530                 sout_InputNew( p_owner->p_sout,
1531                                &p_owner->sout );
1532
1533             if( p_owner->p_sout_input == NULL )
1534             {
1535                 msg_Err( p_dec, "cannot create packetizer output (%4.4s)",
1536                          (char *)&p_owner->sout.i_codec );
1537                 p_dec->b_error = true;
1538
1539                 while( p_sout_block )
1540                 {
1541                     block_t *p_next = p_sout_block->p_next;
1542                     block_Release( p_sout_block );
1543                     p_sout_block = p_next;
1544                 }
1545                 break;
1546             }
1547         }
1548
1549         while( p_sout_block )
1550         {
1551             block_t *p_next = p_sout_block->p_next;
1552
1553             p_sout_block->p_next = NULL;
1554
1555             DecoderPlaySout( p_dec, p_sout_block, b_telx );
1556
1557             p_sout_block = p_next;
1558         }
1559
1560         /* For now it's enough, as only sout impact on this flag */
1561         if( p_owner->p_sout->i_out_pace_nocontrol > 0 &&
1562             p_owner->p_input->p->b_out_pace_control )
1563         {
1564             msg_Dbg( p_dec, "switching to sync mode" );
1565             p_owner->p_input->p->b_out_pace_control = false;
1566         }
1567         else if( p_owner->p_sout->i_out_pace_nocontrol <= 0 &&
1568                  !p_owner->p_input->p->b_out_pace_control )
1569         {
1570             msg_Dbg( p_dec, "switching to async mode" );
1571             p_owner->p_input->p->b_out_pace_control = true;
1572         }
1573     }
1574 }
1575
1576 /* This function process a video block
1577  */
1578 static void DecoderProcessVideo( decoder_t *p_dec, block_t *p_block, bool b_flush )
1579 {
1580     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1581
1582     if( p_owner->p_packetizer )
1583     {
1584         block_t *p_packetized_block;
1585         decoder_t *p_packetizer = p_owner->p_packetizer;
1586
1587         while( (p_packetized_block =
1588                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1589         {
1590             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1591             {
1592                 es_format_Clean( &p_dec->fmt_in );
1593                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1594             }
1595             if( p_packetizer->pf_get_cc )
1596                 DecoderGetCc( p_dec, p_packetizer );
1597
1598             while( p_packetized_block )
1599             {
1600                 block_t *p_next = p_packetized_block->p_next;
1601                 p_packetized_block->p_next = NULL;
1602
1603                 DecoderDecodeVideo( p_dec, p_packetized_block );
1604
1605                 p_packetized_block = p_next;
1606             }
1607         }
1608     }
1609     else if( p_block )
1610     {
1611         DecoderDecodeVideo( p_dec, p_block );
1612     }
1613
1614     if( b_flush && p_owner->p_vout )
1615         VoutFlushPicture( p_owner->p_vout, 1 );
1616 }
1617
1618 /* This function process a audio block
1619  */
1620 static void DecoderProcessAudio( decoder_t *p_dec, block_t *p_block, bool b_flush )
1621 {
1622     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1623
1624     if( p_owner->p_packetizer )
1625     {
1626         block_t *p_packetized_block;
1627         decoder_t *p_packetizer = p_owner->p_packetizer;
1628
1629         while( (p_packetized_block =
1630                 p_packetizer->pf_packetize( p_packetizer, p_block ? &p_block : NULL )) )
1631         {
1632             if( p_packetizer->fmt_out.i_extra && !p_dec->fmt_in.i_extra )
1633             {
1634                 es_format_Clean( &p_dec->fmt_in );
1635                 es_format_Copy( &p_dec->fmt_in, &p_packetizer->fmt_out );
1636             }
1637
1638             while( p_packetized_block )
1639             {
1640                 block_t *p_next = p_packetized_block->p_next;
1641                 p_packetized_block->p_next = NULL;
1642
1643                 DecoderDecodeAudio( p_dec, p_packetized_block );
1644
1645                 p_packetized_block = p_next;
1646             }
1647         }
1648     }
1649     else if( p_block )
1650     {
1651         DecoderDecodeAudio( p_dec, p_block );
1652     }
1653
1654     if( b_flush && p_owner->p_aout && p_owner->p_aout_input )
1655         aout_DecFlush( p_owner->p_aout, p_owner->p_aout_input );
1656 }
1657
1658 /* This function process a subtitle block
1659  */
1660 static void DecoderProcessSpu( decoder_t *p_dec, block_t *p_block, bool b_flush )
1661 {
1662     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1663     const bool b_telx = p_dec->fmt_in.i_codec == VLC_FOURCC('t','e','l','x');
1664
1665     input_thread_t *p_input = p_owner->p_input;
1666     vout_thread_t *p_vout;
1667     subpicture_t *p_spu;
1668
1669     while( (p_spu = p_dec->pf_decode_sub( p_dec, p_block ? &p_block : NULL ) ) )
1670     {
1671         vlc_mutex_lock( &p_input->p->counters.counters_lock );
1672         stats_UpdateInteger( p_dec, p_input->p->counters.p_decoded_sub, 1, NULL );
1673         vlc_mutex_unlock( &p_input->p->counters.counters_lock );
1674
1675         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1676         if( p_vout && p_owner->p_spu_vout == p_vout )
1677         {
1678             /* Preroll does not work very well with subtitle */
1679             if( p_spu->i_start > 0 &&
1680                 p_spu->i_start < p_owner->i_preroll_end &&
1681                 ( p_spu->i_stop <= 0 || p_spu->i_stop < p_owner->i_preroll_end ) )
1682             {
1683                 subpicture_Delete( p_spu );
1684             }
1685             else
1686             {
1687                 DecoderPlaySpu( p_dec, p_spu, b_telx );
1688             }
1689         }
1690         else
1691         {
1692             subpicture_Delete( p_spu );
1693         }
1694         if( p_vout )
1695             vlc_object_release( p_vout );
1696     }
1697
1698     if( b_flush && p_owner->p_spu_vout )
1699     {
1700         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1701
1702         if( p_vout && p_owner->p_spu_vout == p_vout )
1703             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1704                          p_owner->i_spu_channel );
1705
1706         if( p_vout )
1707             vlc_object_release( p_vout );
1708     }
1709 }
1710
1711 /**
1712  * Decode a block
1713  *
1714  * \param p_dec the decoder object
1715  * \param p_block the block to decode
1716  * \return VLC_SUCCESS or an error code
1717  */
1718 static int DecoderProcess( decoder_t *p_dec, block_t *p_block )
1719 {
1720     decoder_owner_sys_t *p_owner = (decoder_owner_sys_t *)p_dec->p_owner;
1721     const bool b_flush_request = p_block && (p_block->i_flags & BLOCK_FLAG_CORE_FLUSH);
1722
1723     if( p_block && p_block->i_buffer <= 0 )
1724     {
1725         assert( !b_flush_request );
1726         block_Release( p_block );
1727         return VLC_SUCCESS;
1728     }
1729
1730 #ifdef ENABLE_SOUT
1731     if( p_dec->i_object_type == VLC_OBJECT_PACKETIZER )
1732     {
1733         if( p_block )
1734             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1735
1736         DecoderProcessSout( p_dec, p_block );
1737     }
1738     else
1739 #endif
1740     {
1741         bool b_flush = false;
1742
1743         if( p_block )
1744         {
1745             const bool b_flushing = p_owner->i_preroll_end == INT64_MAX;
1746             DecoderUpdatePreroll( &p_owner->i_preroll_end, p_block );
1747
1748             b_flush = !b_flushing && b_flush_request;
1749
1750             p_block->i_flags &= ~BLOCK_FLAG_CORE_PRIVATE_MASK;
1751         }
1752
1753         if( p_dec->fmt_in.i_cat == AUDIO_ES )
1754         {
1755             DecoderProcessAudio( p_dec, p_block, b_flush );
1756         }
1757         else if( p_dec->fmt_in.i_cat == VIDEO_ES )
1758         {
1759             DecoderProcessVideo( p_dec, p_block, b_flush );
1760         }
1761         else if( p_dec->fmt_in.i_cat == SPU_ES )
1762         {
1763             DecoderProcessSpu( p_dec, p_block, b_flush );
1764         }
1765         else
1766         {
1767             msg_Err( p_dec, "unknown ES format" );
1768             p_dec->b_error = true;
1769         }
1770     }
1771
1772     /* */
1773     if( b_flush_request )
1774     {
1775         vlc_mutex_lock( &p_owner->lock );
1776         DecoderFlushBuffering( p_dec );
1777         vlc_mutex_unlock( &p_owner->lock );
1778
1779         DecoderSignalFlushed( p_dec );
1780     }
1781
1782     return p_dec->b_error ? VLC_EGENERIC : VLC_SUCCESS;
1783 }
1784
1785 /**
1786  * Destroys a decoder object
1787  *
1788  * \param p_dec the decoder object
1789  * \return nothing
1790  */
1791 static void DeleteDecoder( decoder_t * p_dec )
1792 {
1793     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1794
1795     msg_Dbg( p_dec, "killing decoder fourcc `%4.4s', %u PES in FIFO",
1796              (char*)&p_dec->fmt_in.i_codec,
1797              (unsigned)block_FifoCount( p_owner->p_fifo ) );
1798
1799     /* Free all packets still in the decoder fifo. */
1800     block_FifoEmpty( p_owner->p_fifo );
1801     block_FifoRelease( p_owner->p_fifo );
1802
1803     /* */
1804     vlc_mutex_lock( &p_owner->lock );
1805     DecoderFlushBuffering( p_dec );
1806     vlc_mutex_unlock( &p_owner->lock );
1807
1808     /* Cleanup */
1809     if( p_owner->p_aout_input )
1810         aout_DecDelete( p_owner->p_aout, p_owner->p_aout_input );
1811     if( p_owner->p_aout )
1812     {
1813         vlc_object_release( p_owner->p_aout );
1814         p_owner->p_aout = NULL;
1815     }
1816     if( p_owner->p_vout )
1817     {
1818         /* Hack to make sure all the the pictures are freed by the decoder */
1819         for( int i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
1820         {
1821             picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
1822
1823             if( p_pic->i_status == RESERVED_PICTURE )
1824                 vout_DestroyPicture( p_owner->p_vout, p_pic );
1825             if( p_pic->i_refcount > 0 )
1826                 vout_UnlinkPicture( p_owner->p_vout, p_pic );
1827         }
1828
1829         /* We are about to die. Reattach video output to p_vlc. */
1830         vout_Request( p_dec, p_owner->p_vout, NULL );
1831         var_SetBool( p_owner->p_input, "intf-change-vout", true );
1832     }
1833
1834 #ifdef ENABLE_SOUT
1835     if( p_owner->p_sout_input )
1836     {
1837         sout_InputDelete( p_owner->p_sout_input );
1838         es_format_Clean( &p_owner->sout );
1839     }
1840 #endif
1841
1842     if( p_dec->fmt_in.i_cat == SPU_ES )
1843     {
1844         vout_thread_t *p_vout;
1845
1846         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
1847         if( p_vout && p_owner->p_spu_vout == p_vout )
1848         {
1849             spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR,
1850                          p_owner->i_spu_channel );
1851             vlc_object_release( p_vout );
1852         }
1853     }
1854
1855     es_format_Clean( &p_dec->fmt_in );
1856     es_format_Clean( &p_dec->fmt_out );
1857
1858     if( p_owner->p_packetizer )
1859     {
1860         module_unneed( p_owner->p_packetizer,
1861                        p_owner->p_packetizer->p_module );
1862         es_format_Clean( &p_owner->p_packetizer->fmt_in );
1863         es_format_Clean( &p_owner->p_packetizer->fmt_out );
1864         vlc_object_detach( p_owner->p_packetizer );
1865         vlc_object_release( p_owner->p_packetizer );
1866     }
1867
1868     vlc_cond_destroy( &p_owner->wait );
1869     vlc_mutex_destroy( &p_owner->lock );
1870
1871     vlc_object_detach( p_dec );
1872
1873     free( p_owner );
1874 }
1875
1876 /*****************************************************************************
1877  * Buffers allocation callbacks for the decoders
1878  *****************************************************************************/
1879 static aout_buffer_t *aout_new_buffer( decoder_t *p_dec, int i_samples )
1880 {
1881     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1882     aout_buffer_t *p_buffer;
1883
1884     if( p_owner->p_aout_input != NULL &&
1885         ( p_dec->fmt_out.audio.i_rate != p_owner->audio.i_rate ||
1886           p_dec->fmt_out.audio.i_original_channels !=
1887               p_owner->audio.i_original_channels ||
1888           p_dec->fmt_out.audio.i_bytes_per_frame !=
1889               p_owner->audio.i_bytes_per_frame ) )
1890     {
1891         aout_input_t *p_aout_input = p_owner->p_aout_input;
1892
1893         /* Parameters changed, restart the aout */
1894         vlc_mutex_lock( &p_owner->lock );
1895
1896         DecoderFlushBuffering( p_dec );
1897
1898         p_owner->p_aout_input = NULL;
1899         aout_DecDelete( p_owner->p_aout, p_aout_input );
1900
1901         vlc_mutex_unlock( &p_owner->lock );
1902     }
1903
1904     if( p_owner->p_aout_input == NULL )
1905     {
1906         const int i_force_dolby = config_GetInt( p_dec, "force-dolby-surround" );
1907         audio_sample_format_t format;
1908         aout_input_t *p_aout_input;
1909         aout_instance_t *p_aout;
1910
1911         p_dec->fmt_out.audio.i_format = p_dec->fmt_out.i_codec;
1912         p_owner->audio = p_dec->fmt_out.audio;
1913
1914         memcpy( &format, &p_owner->audio, sizeof( audio_sample_format_t ) );
1915         if ( i_force_dolby && (format.i_original_channels&AOUT_CHAN_PHYSMASK)
1916                                     == (AOUT_CHAN_LEFT|AOUT_CHAN_RIGHT) )
1917         {
1918             if ( i_force_dolby == 1 )
1919             {
1920                 format.i_original_channels = format.i_original_channels |
1921                                              AOUT_CHAN_DOLBYSTEREO;
1922             }
1923             else /* i_force_dolby == 2 */
1924             {
1925                 format.i_original_channels = format.i_original_channels &
1926                                              ~AOUT_CHAN_DOLBYSTEREO;
1927             }
1928         }
1929
1930         p_aout = p_owner->p_aout;
1931         p_aout_input = aout_DecNew( p_dec, &p_aout,
1932                                     &format, &p_dec->fmt_out.audio_replay_gain );
1933
1934         vlc_mutex_lock( &p_owner->lock );
1935         p_owner->p_aout = p_aout;
1936         p_owner->p_aout_input = p_aout_input;
1937         vlc_mutex_unlock( &p_owner->lock );
1938
1939         if( p_owner->p_aout_input == NULL )
1940         {
1941             msg_Err( p_dec, "failed to create audio output" );
1942             p_dec->b_error = true;
1943             return NULL;
1944         }
1945         p_dec->fmt_out.audio.i_bytes_per_frame =
1946             p_owner->audio.i_bytes_per_frame;
1947     }
1948
1949     p_buffer = aout_DecNewBuffer( p_owner->p_aout_input, i_samples );
1950
1951     return p_buffer;
1952 }
1953
1954 static void aout_del_buffer( decoder_t *p_dec, aout_buffer_t *p_buffer )
1955 {
1956     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1957
1958     aout_DecDeleteBuffer( p_owner->p_aout,
1959                           p_owner->p_aout_input, p_buffer );
1960 }
1961
1962
1963 int vout_CountPictureAvailable( vout_thread_t *p_vout );
1964
1965 static picture_t *vout_new_buffer( decoder_t *p_dec )
1966 {
1967     decoder_owner_sys_t *p_owner = p_dec->p_owner;
1968
1969     if( p_owner->p_vout == NULL ||
1970         p_dec->fmt_out.video.i_width != p_owner->video.i_width ||
1971         p_dec->fmt_out.video.i_height != p_owner->video.i_height ||
1972         p_dec->fmt_out.video.i_chroma != p_owner->video.i_chroma ||
1973         p_dec->fmt_out.video.i_aspect != p_owner->video.i_aspect )
1974     {
1975         vout_thread_t *p_vout;
1976
1977         if( !p_dec->fmt_out.video.i_width ||
1978             !p_dec->fmt_out.video.i_height )
1979         {
1980             /* Can't create a new vout without display size */
1981             return NULL;
1982         }
1983
1984         if( !p_dec->fmt_out.video.i_visible_width ||
1985             !p_dec->fmt_out.video.i_visible_height )
1986         {
1987             if( p_dec->fmt_in.video.i_visible_width &&
1988                 p_dec->fmt_in.video.i_visible_height )
1989             {
1990                 p_dec->fmt_out.video.i_visible_width =
1991                     p_dec->fmt_in.video.i_visible_width;
1992                 p_dec->fmt_out.video.i_visible_height =
1993                     p_dec->fmt_in.video.i_visible_height;
1994             }
1995             else
1996             {
1997                 p_dec->fmt_out.video.i_visible_width =
1998                     p_dec->fmt_out.video.i_width;
1999                 p_dec->fmt_out.video.i_visible_height =
2000                     p_dec->fmt_out.video.i_height;
2001             }
2002         }
2003
2004         if( p_dec->fmt_out.video.i_visible_height == 1088 &&
2005             var_CreateGetBool( p_dec, "hdtv-fix" ) )
2006         {
2007             p_dec->fmt_out.video.i_visible_height = 1080;
2008             p_dec->fmt_out.video.i_sar_num *= 135;
2009             p_dec->fmt_out.video.i_sar_den *= 136;
2010             msg_Warn( p_dec, "Fixing broken HDTV stream (display_height=1088)");
2011         }
2012
2013         if( !p_dec->fmt_out.video.i_sar_num ||
2014             !p_dec->fmt_out.video.i_sar_den )
2015         {
2016             p_dec->fmt_out.video.i_sar_num = p_dec->fmt_out.video.i_aspect *
2017               p_dec->fmt_out.video.i_visible_height;
2018
2019             p_dec->fmt_out.video.i_sar_den = VOUT_ASPECT_FACTOR *
2020               p_dec->fmt_out.video.i_visible_width;
2021         }
2022
2023         vlc_ureduce( &p_dec->fmt_out.video.i_sar_num,
2024                      &p_dec->fmt_out.video.i_sar_den,
2025                      p_dec->fmt_out.video.i_sar_num,
2026                      p_dec->fmt_out.video.i_sar_den, 50000 );
2027
2028         p_dec->fmt_out.video.i_chroma = p_dec->fmt_out.i_codec;
2029         p_owner->video = p_dec->fmt_out.video;
2030
2031         vlc_mutex_lock( &p_owner->lock );
2032
2033         DecoderFlushBuffering( p_dec );
2034
2035         p_vout = p_owner->p_vout;
2036         p_owner->p_vout = NULL;
2037         vlc_mutex_unlock( &p_owner->lock );
2038
2039         p_vout = vout_Request( p_dec, p_vout, &p_dec->fmt_out.video );
2040
2041         vlc_mutex_lock( &p_owner->lock );
2042         p_owner->p_vout = p_vout;
2043         vlc_mutex_unlock( &p_owner->lock );
2044
2045         var_SetBool( p_owner->p_input, "intf-change-vout", true );
2046         if( p_vout == NULL )
2047         {
2048             msg_Err( p_dec, "failed to create video output" );
2049             p_dec->b_error = true;
2050             return NULL;
2051         }
2052
2053         if( p_owner->video.i_rmask )
2054             p_owner->p_vout->render.i_rmask = p_owner->video.i_rmask;
2055         if( p_owner->video.i_gmask )
2056             p_owner->p_vout->render.i_gmask = p_owner->video.i_gmask;
2057         if( p_owner->video.i_bmask )
2058             p_owner->p_vout->render.i_bmask = p_owner->video.i_bmask;
2059     }
2060
2061     /* Get a new picture
2062      */
2063     for( ;; )
2064     {
2065         picture_t *p_picture;
2066         int i_pic, i_ready_pic;
2067
2068         if( p_dec->b_die || p_dec->b_error )
2069             return NULL;
2070
2071         /* The video filter chain required that there is always 1 free buffer
2072          * that it will use as temporary one. It will release the temporary
2073          * buffer once its work is done, so this check is safe even if we don't
2074          * lock around both count() and create().
2075          */
2076         if( vout_CountPictureAvailable( p_owner->p_vout ) >= 2 )
2077         {
2078             p_picture = vout_CreatePicture( p_owner->p_vout, 0, 0, 0 );
2079             if( p_picture )
2080                 return p_picture;
2081         }
2082
2083         if( DecoderIsFlushing( p_dec ) )
2084             return NULL;
2085
2086         /* */
2087         DecoderSignalBuffering( p_dec, true );
2088
2089         /* Check the decoder doesn't leak pictures */
2090         for( i_pic = 0, i_ready_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
2091         {
2092             const picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
2093
2094             if( p_pic->i_status == READY_PICTURE )
2095             {
2096                 i_ready_pic++;
2097                 /* If we have at least 2 ready pictures, wait for the vout thread to
2098                  * process one */
2099                 if( i_ready_pic >= 2 )
2100                     break;
2101
2102                 continue;
2103             }
2104
2105             if( p_pic->i_status == DISPLAYED_PICTURE )
2106             {
2107                 /* If at least one displayed picture is not referenced
2108                  * let vout free it */
2109                 if( p_pic->i_refcount == 0 )
2110                     break;
2111             }
2112         }
2113
2114         if( i_pic == p_owner->p_vout->render.i_pictures )
2115         {
2116             /* Too many pictures are still referenced, there is probably a bug
2117              * with the decoder */
2118             msg_Err( p_dec, "decoder is leaking pictures, resetting the heap" );
2119
2120             /* Just free all the pictures */
2121             for( i_pic = 0; i_pic < p_owner->p_vout->render.i_pictures; i_pic++ )
2122             {
2123                 picture_t *p_pic = p_owner->p_vout->render.pp_picture[i_pic];
2124
2125                 if( p_pic->i_status == RESERVED_PICTURE )
2126                     vout_DestroyPicture( p_owner->p_vout, p_pic );
2127                 if( p_pic->i_refcount > 0 )
2128                     vout_UnlinkPicture( p_owner->p_vout, p_pic );
2129             }
2130         }
2131
2132         msleep( VOUT_OUTMEM_SLEEP );
2133     }
2134 }
2135
2136 static void vout_del_buffer( decoder_t *p_dec, picture_t *p_pic )
2137 {
2138     VoutDisplayedPicture( p_dec->p_owner->p_vout, p_pic );
2139 }
2140
2141 static void vout_link_picture( decoder_t *p_dec, picture_t *p_pic )
2142 {
2143     vout_LinkPicture( p_dec->p_owner->p_vout, p_pic );
2144 }
2145
2146 static void vout_unlink_picture( decoder_t *p_dec, picture_t *p_pic )
2147 {
2148     vout_UnlinkPicture( p_dec->p_owner->p_vout, p_pic );
2149 }
2150
2151 static subpicture_t *spu_new_buffer( decoder_t *p_dec )
2152 {
2153     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2154     vout_thread_t *p_vout = NULL;
2155     subpicture_t *p_subpic;
2156     int i_attempts = 30;
2157
2158     while( i_attempts-- )
2159     {
2160         if( p_dec->b_die || p_dec->b_error )
2161             break;
2162
2163         p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2164         if( p_vout )
2165             break;
2166
2167         msleep( VOUT_DISPLAY_DELAY );
2168     }
2169
2170     if( !p_vout )
2171     {
2172         msg_Warn( p_dec, "no vout found, dropping subpicture" );
2173         return NULL;
2174     }
2175
2176     if( p_owner->p_spu_vout != p_vout )
2177     {
2178         vlc_mutex_lock( &p_owner->lock );
2179
2180         DecoderFlushBuffering( p_dec );
2181
2182         vlc_mutex_unlock( &p_owner->lock );
2183
2184         spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
2185                      &p_owner->i_spu_channel );
2186         p_owner->i_spu_order = 0;
2187         p_owner->p_spu_vout = p_vout;
2188     }
2189
2190     p_subpic = subpicture_New();
2191     if( p_subpic )
2192     {
2193         p_subpic->i_channel = p_owner->i_spu_channel;
2194         p_subpic->i_order = p_owner->i_spu_order++;
2195         p_subpic->b_subtitle = true;
2196     }
2197
2198     vlc_object_release( p_vout );
2199
2200     return p_subpic;
2201 }
2202
2203 static void spu_del_buffer( decoder_t *p_dec, subpicture_t *p_subpic )
2204 {
2205     decoder_owner_sys_t *p_owner = p_dec->p_owner;
2206     vout_thread_t *p_vout = NULL;
2207
2208     p_vout = vlc_object_find( p_dec, VLC_OBJECT_VOUT, FIND_ANYWHERE );
2209     if( !p_vout || p_owner->p_spu_vout != p_vout )
2210     {
2211         if( p_vout )
2212             vlc_object_release( p_vout );
2213         msg_Warn( p_dec, "no vout found, leaking subpicture" );
2214         return;
2215     }
2216
2217     subpicture_Delete( p_subpic );
2218
2219     vlc_object_release( p_vout );
2220 }
2221