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