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