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